Key reading in a Window
Key reading in a Window
For the life of me I can't figure out how to read input coming from the keyboard without opening a screen, windowed screen, or a console, and of course the program I've been working on is using a plain Window. I've tried adding a keyboard shortcut, using a canvas gadget, google, this site, the PDF on how to code in Pure Basic, but I still can't find a way to get key-by-key input from the user without having to spend a long time learning how to use screens (or windowed screens), and rewriting most of my program.
- netmaestro
- PureBasic Bullfrog

- Posts: 8452
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
Re: Key reading in a Window
1) What sort of keystrokes, for what?
2) What OS(s) are you interested in targeting?
2) What OS(s) are you interested in targeting?
BERESHEIT
Re: Key reading in a Window
I'm making block breaker and am using the escape key for an ingame menu. Also possibly future cheat codes. Windows is the OS.
- netmaestro
- PureBasic Bullfrog

- Posts: 8452
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
Re: Key reading in a Window
The CanvasGadget is well suited to a block breaker and it supports the keyboard fully. Here's a basic sample:
Code: Select all
wMain = OpenWindow(#PB_Any,0,0,800,600,"",#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_SizeGadget|#PB_Window_MinimizeGadget)
CanvasGadget(0,0,0,800,600,#PB_Canvas_Keyboard)
SetActiveGadget(0)
quit=0
Repeat
EventID = WaitWindowEvent()
Select EventID
Case #PB_Event_CloseWindow
quit=1
Case #PB_Event_Gadget
thisgadget = EventGadget()
thiseventtype = EventType()
Select thisgadget
Case 0
If thiseventtype = #PB_EventType_KeyDown
key = GetGadgetAttribute(0, #PB_Canvas_Key)
Debug "keydown: " + Str(key)
ElseIf thiseventtype = #PB_EventType_KeyUp
key = GetGadgetAttribute(0, #PB_Canvas_Key)
Debug "keyup: " + Str(key)
EndIf
EndSelect
EndSelect
Until quit
BERESHEIT
Re: Key reading in a Window
I think he's after a keybooard hook to the OS not just for a window his application owns.
Re: Key reading in a Window
Somehow I remembered the canvas gadget for mouse tracking, but not the one for keyboard tracking. Yes, this is what I was after, thank you!

