keyboard events

Share your advanced PureBasic knowledge/code with the community.
User avatar
waffle
Enthusiast
Enthusiast
Posts: 129
Joined: Mon May 12, 2003 1:34 pm
Location: USA
Contact:

keyboard events

Post by waffle »

Code updated For 5.20+

I had an odd bug and just figured out the problem,
and thought to share this :)

Code: Select all


If InitSprite() = 0 Or InitKeyboard() = 0
  MessageRequester("Header Error", "Can't open DirectX 7 or later", 0)
  End
EndIf
If OpenWindow(0,0,0,300,200, "bug test",#PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_TitleBar | #PB_Window_ScreenCentered)
  
  If OpenWindowedScreen(WindowID(0),0, 0, 300,200,0,0,0 )
    noquit=1
    While noquit
      we.l=WaitWindowEvent()
      ExamineKeyboard()
      If KeyboardPushed(#PB_Key_Escape)
        noquit=0
      EndIf
      key.s=KeyboardInkey()
      If key<>""
        Debug key
      EndIf
      Delay(1)
    Wend
  EndIf
EndIf
End

the bug is that pressing the keyboard does not
generate a window event and that took me awhile to
find out what was not happening :)

Probably a non issue when using gadgets though, as they
would detect keypresses themselves.
Code is good... Its an international language.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: keyboard events

Post by PB »

> pressing the keyboard does not generate a window event

You can use the #WM_KEYDOWN event in a callback to catch keystrokes:

Code: Select all

Procedure MyWindowCallback(WindowID,Message,wParam,lParam)
  Result=#PB_ProcessPureBasicEvents
  If Message=#WM_KEYDOWN
    Debug "Asc = "+Str(wParam)
  EndIf
  ProcedureReturn Result
EndProcedure

If OpenWindow(0,100,150,400,200,#PB_Window_SystemMenu,"test")
  SetWindowCallback(@MyWindowCallback())
  Repeat
    ev=WaitWindowEvent()
  Until ev=#PB_EventCloseWindow
EndIf
User avatar
waffle
Enthusiast
Enthusiast
Posts: 129
Joined: Mon May 12, 2003 1:34 pm
Location: USA
Contact:

Post by waffle »

yea, that works too,
But I'm using a buffered screen anyway,
so i'm doing the old windowevent with the delay(1) thing.

Basically, I'm writing a map editor based on paul's tool
for hostile skys, only mine can import any bitmap
and has other features... any tile size and so on.

I may even add mouse support. So far it looks pretty good.

I'll place your code into my library.... it will get used :)

edit:
dropped in your code and also added mouse detection
so my map editor has full mouse support. Thanks again.
Code is good... Its an international language.
Post Reply