How to detect keyboard press and toggle events
Posted: Mon Dec 08, 2008 11:09 am
I've seen a lot of variations of this on the forum so I whipped up this example to demonstrate how the significant bits of the GetKeyState_() result actually work.
For more information on GetKeyState vs GetAsyncKeyState see here:
http://blogs.msdn.com/oldnewthing/archi ... 72262.aspx
Here is an example for detecting mouse events and returning their result in a bitmask:
Code: Select all
OpenWindow(0,0,0,160,120,"")
TextGadget(0,8,8,80,20,"")
TextGadget(1,8,32,80,20,"")
TextGadget(2,8,48,80,20,"")
Repeat
KeyState.w=GetKeyState_(#VK_SHIFT)
SetGadgetText(0,"Binary "+LSet(Right(Bin(KeyState),8),8,"0"))
If KeyState&%10000000
KeyPressed=1
Else
KeyPressed=0
EndIf
SetGadgetText(1,"KeyState "+Str(KeyPressed))
If KeyState&%00000001
ToggleState=1
Else
ToggleState=0
EndIf
SetGadgetText(2,"ToggleState "+Str(ToggleState))
Until WaitWindowEvent(100)=#WM_CLOSE
End
http://blogs.msdn.com/oldnewthing/archi ... 72262.aspx
Here is an example for detecting mouse events and returning their result in a bitmask:
Code: Select all
Procedure MouseClick()
If GetKeyState_(#VK_LBUTTON)&%10000000
LButton=1
EndIf
If GetKeyState_(#VK_MBUTTON)&%10000000
MButton=2
EndIf
If GetKeyState_(#VK_RBUTTON)&%10000000
RButton=4
EndIf
If GetKeyState_($05)&%10000000
XButton1=8
EndIf
If GetKeyState_($06)&%10000000
XButton2=16
EndIf
ProcedureReturn (LButton|MButton|RButton|XButton1|XButton2)
EndProcedure