You are not logged in. You can browse in the PowerBASIC Community, but you must click Login (top right) before you can post. If this is your first visit, check out the FAQ or Sign Up.
I am just starting to use pbcc40 to update a dos version of a program. Since I don't want to reinvent the wheel does somebody have some code the I could copy to handle the mouse in pbcc?
Thanks in advance.
Jack Jasperson
It makes a difference whether you want to use the mouse in the console window or in a graphic window.
Fred's and Michael's replies refer to the use of the mouse in a console window.
PBCC 5 also supports the use of the mouse in a graphic window, but PBCC 4 does not.
However it is very well possible to use the mouse in a graphic window with PBCC 4 using a technique that is called subclassing.
I once posted a set of screen handlers that demonstrate this at http://www.powerbasic.com/support/pb...ad.php?t=36795
>I still haven't found a good way to read a key or mouse event at the same time.
???
They don't happen at the same time.
You get one, you handle it. You get the other, you handle it.
???
I think you might want INKEY instead of WAITKEY.. WAITKEY 'works as advertised' and WAITS UNTIL A KEY is hit.
Maybe you need something like INKEY which returns immediately...
Code:
IF INKEY = NO KEY THEN
Check for mouse click
....
.. or something like that.
I am NOT a CC guy leave alone a CC "GRAPHICS" guy but I have to believe someone has written CC-GRAPHICS code which can handle keys and/or mouses at the same time.
Yes Manuel. I was looking for something simpler - maybe there is not!
I think Michael's answer may send me back to your code.
"They don't happen at the same time.
You get one, you handle it. You get the other, you handle it."
That's not the way I learned to program (I'm too linear).
I was using Graphic Waitkey$ to get input, but also wanted to know if a mouse event occured. The Inkey is most likely the answer. I wrote a routine to try that, but the screen flashes at the input point and I may be hogging computer resource.
It just seems like it would be simpler, but maybe not.
It would have to continuously scan for mouse or keyboard input. Would you need to release the procedure on each pass?
You have to use a bit of subclassing to make it work.
Here is the way to do it :
Code:
#COMPILE EXE
#BREAK ON
#CONSOLE OFF
#DEBUG ERROR ON ' Remove in final version
#INCLUDE "Win32Api.inc"
DEFLNG a-z
GLOBAL GrDialogProc AS LONG
GLOBAL GrStaticProc AS LONG
GLOBAL mx AS LONG,my AS LONG ' Mouse x and y
GLOBAL lb AS LONG,rb AS LONG ' Left and right mouse button
GLOBAL mm AS LONG ' Detect mouse movements
GLOBAL bg AS LONG,fg AS LONG ' Background and foreground colors
GLOBAL wm AS LONG,mb AS LONG ' Wheel mouse and middle button
GLOBAL TabKey AS INTEGER ' Detect Tab Key
FUNCTION GrDlgProc(BYVAL hWnd AS DWORD, BYVAL wMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG
FUNCTION = CallWindowProc(GrDialogProc, hWnd, wMsg, wParam, lParam)
END FUNCTION
FUNCTION GrProc(BYVAL hWnd AS DWORD, BYVAL wMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG
LOCAL p AS pointapi
SELECT CASE wMsg
CASE %WM_CHAR
car%=LO(WORD,wParam): IF car%=9 THEN TabKey%=9 ' Tab Key not caught by GRAPHIC INKEY$
CASE %WM_MOUSEMOVE
mm=1: mx=LO(WORD,lParam): my=HI(WORD,lParam) ' Current Mouse X and Y Position in the graphic window
CASE %WM_LBUTTONDOWN
lb=1: FUNCTION=0: EXIT FUNCTION ' Left button pressed
CASE %WM_RBUTTONDOWN
rb=1: FUNCTION=0: EXIT FUNCTION ' Right button pressed
CASE %WM_MBUTTONDOWN
mb=1: FUNCTION=0: EXIT FUNCTION ' Middle button pressed
CASE %WM_MOUSEWHEEL
wm=HI(WORD,wParam): IF wm>32768 THEN wm=-1 ELSE wm=1 ' Wheel turned (+)=up (-)=down
FUNCTION=0: EXIT FUNCTION
END SELECT
FUNCTION = CallWindowProc(GrStaticProc, hWnd, wMsg, wParam, lParam)
END FUNCTION
Put that code before your PBMAIN() and you will even enjoy the wheel mouse also.
If you want a complete program check on my GRAPHIC CONSOLE post
Thanks for your reply. Regarding your concern about hijacking Jack’s thread, hopefully this subject is of general interest, including him if he eventually gets some interest in PBCC5’s GRAPHIC WINDOW.
Risking to be too obvious, two points are very important: First, the option “REDRAW” when you start with a graphic window, and second, to enclose the sequence within a loop whose exit is granted only for a key press OR a mouse click (the program is waiting for user’s action).
Code:
[SIZE=3][FONT=Calibri]DO[/FONT][/SIZE]
[SIZE=3][FONT=Calibri] GRAPHIC INKEY$ TO CH$[/FONT][/SIZE]
[SIZE=3][FONT=Calibri] GRAPHIC WINDOW CLICK TO clk&, xm!, ym![/FONT][/SIZE]
[SIZE=3][FONT=Calibri] IF CH$<>"" OR clk& THEN EXIT LOOP[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]LOOP[/FONT][/SIZE]
This way nothing happens until one of the two events occurs. You can monitor the use of resources in the meanwhile, and you will see that it’s OK.
I have just released a program (2,500 lines of code) using frequently the same statements and I haven’t noticed what you point out. You can also check out a code I posted in the Source Code section named Color Display Applet .
Even though I got encouraged to work in PBCC’ GRAPHIC WINDOW because of Guy Dombrosky’s posts on the subject, I prefer not to use the #INCLUDE "Win32Api.inc" statement. In my opinion to detect the mouse moving, or the mouse wheel action is not really necessary for a good program.
With all due respect for Michael Mattias, whose professorship in my opinion is highly appreciated by everybody, I have to admit my preferences for the “procedural programming” instead of the “event driven programming”.
Last edited by Manuel Valdes; 22 Jul 2009, 10:26 PM.
Well.... I am merely a caveman programmer, but this works for me.
I used Manuel's simple approach ... though I reserve the right to read the scroll wheel etc. at some future date (given the right amount of inspiration).
(This is incomplete as I'm not using all the keys - it's just to get what I want.)
Code:
Function ikeys(w1%,mox&,moy&)As Integer
Local i$,ink%,ink2%,clk&,xm!,ym! 'w1% is for later (advanced mouse functions)
Do
Do
Sleep 0 'keep the loop from overusing resource
Graphic INKEY$ To i$
Graphic Window Click To clk&, xm!, ym!
If i$<>"" Or clk& Then Exit Loop 'one or the other
Loop
ink2=Asc(Left$(i$,1)):ink=Asc(Right$(i$,1))
If ink<>ink2 Then ink=ink+300 'move special keys to 300 range
If ink >= 97 And ink <= 122 Then ink = ink - 32 'take out if you need lowercase as well
'Graphic Set Pos (10,0)
'Graphic Print Len(i$);ink;ink2;clk&;xm!;ym!
'Graphic ReDraw 'only in to view keys hit and mouse position
Select Case ink 'only keep the keys I want
Case -1:mox&=xm!:moy&=ym!
Case 8,9,13,27,32 'used keys
Case 44,43,45,46,48 To 57,61 'numbers and operators
Case 39,91,93,96,123,125 'other used keys
Case 65 To 90 'letters
Case 371,372,373,375,377,379,380,381,382,383 'the arrows moved to the 300 level
Case 359 To 368:ink=ink+42 'f1 to f12 moved to 401-412
Case 387,388:ink=ink+24
Case 1 to 26:ink=200+ink 'ctrl a,b,c etc. moved to 201,202, etc.
Case Else:ink=0
End Select
If ink Then Exit Loop
Loop
ikeys=ink
End Function
Last edited by Doug Ingram; 23 Jul 2009, 01:40 PM.
We process personal data about users of our site, through the use of cookies and other technologies, to deliver our services, and to analyze site activity. For additional details, refer to our Privacy Policy.
By clicking "I AGREE" below, you agree to our Privacy Policy and our personal data processing and cookie practices as described therein. You also acknowledge that this forum may be hosted outside your country and you consent to the collection, storage, and processing of your data in the country where this forum is hosted.
Comment