How to detect keyboard press and toggle events

Share your advanced PureBasic knowledge/code with the community.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

How to detect keyboard press and toggle events

Post by Mistrel »

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.

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
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

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
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

Thanks Mistrel.

I haven't tried it yet, but is something missing from the example code?

EDIT: Got it, this is missing I think

Code: Select all

CreateGadgetList(WindowID(0))
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

That line is only required for 4.20 and below. Sorry it didn't compile for you the first time. :)
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

no problem, im using 4.20 and didn't think about it being that way for 4.3. I appreciate it though, gonna use it in something. :)
User avatar
Blue
Addict
Addict
Posts: 967
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Post by Blue »

Hey, thanks Mistrel,
Now I finally know how to detect the bak and forward buttons on my fancy mouse.

Do you know how to go about detecting in advance the number of buttons present on a mouse?
PB Forums : Proof positive that 2 heads (or more...) are better than one :idea:
LCD
Enthusiast
Enthusiast
Posts: 206
Joined: Sun Jun 01, 2003 10:55 pm
Location: Austria, Vienna
Contact:

Post by LCD »

I want to join to the thanks-thread... It helped me to fix a bug in Retro-X, where unwanted pixels are painted. I know now also too, how to use additional buttons on my mouse.
My PC
Ryzen 9 5950, 64 GB RAM, nVidia RTX A4000, Win 10
Ryzen 7 1700, 32 GB RAM, nVidia RTX A2000, Win 10
Post Reply