Key reading in a Window

Just starting out? Need help? Post your questions and find answers here.
Orreven
User
User
Posts: 10
Joined: Fri Sep 21, 2012 12:47 am

Key reading in a Window

Post 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.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Key reading in a Window

Post by netmaestro »

1) What sort of keystrokes, for what?
2) What OS(s) are you interested in targeting?
BERESHEIT
Orreven
User
User
Posts: 10
Joined: Fri Sep 21, 2012 12:47 am

Re: Key reading in a Window

Post 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.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Key reading in a Window

Post 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
BERESHEIT
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: Key reading in a Window

Post by jassing »

I think he's after a keybooard hook to the OS not just for a window his application owns.
Orreven
User
User
Posts: 10
Joined: Fri Sep 21, 2012 12:47 am

Re: Key reading in a Window

Post 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!
Post Reply