Page 1 of 1

Key reading in a Window

Posted: Thu Feb 21, 2013 3:27 am
by Orreven
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.

Re: Key reading in a Window

Posted: Thu Feb 21, 2013 4:10 am
by netmaestro
1) What sort of keystrokes, for what?
2) What OS(s) are you interested in targeting?

Re: Key reading in a Window

Posted: Thu Feb 21, 2013 4:23 am
by Orreven
I'm making block breaker and am using the escape key for an ingame menu. Also possibly future cheat codes. Windows is the OS.

Re: Key reading in a Window

Posted: Thu Feb 21, 2013 5:00 am
by netmaestro
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

Re: Key reading in a Window

Posted: Thu Feb 21, 2013 5:03 pm
by jassing
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

Posted: Thu Feb 21, 2013 9:28 pm
by Orreven
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!