Input management (windows events)

Share your advanced PureBasic knowledge/code with the community.
Dr. Dri
Enthusiast
Enthusiast
Posts: 243
Joined: Sat Aug 23, 2003 6:45 pm

Input management (windows events)

Post by Dr. Dri »

Code updated for 5.20+

Here is a solution for keyboard handling under windows. I've tried to make it like other PB native events. Indeed, it's just some macros and constants so i made a small resident.

Keyboard.res
[ img]http://purebasic.myftp.org/files/182/Ke ... _count.png[/img]

List of events and associated functions:
  • #PB_Event_KeyboardInkey
    EventInkey()
    EventKeyRepeatCount()
    EventKeyScanCode()
    EventKeyIsExtended()
    EventKeyPreviousState()
    EventKeyTransitionState()
  • #PB_Event_KeyboardReleased
    EventKey()
    EventKeyRepeatCount()
    EventKeyScanCode()
    EventKeyIsExtended()
    EventKeyPreviousState()
  • #PB_Event_KeyboardPushed
    EventKey()
    EventKeyRepeatCount()
    EventKeyScanCode()
    EventKeyIsExtended()
    EventKeyPreviousState()
The EventKey() function returns "#VK_" constants.

And also the code (nothing really exciting...)

Code: Select all

#PB_Event_KeyboardInkey    = #WM_CHAR
#PB_Event_KeyboardReleased = #WM_KEYUP
#PB_Event_KeyboardPushed   = #WM_KEYDOWN

Enumeration ;Key State
  #PB_Key_Released
  #PB_Key_Pushed
EndEnumeration

Macro EventKey()
  EventwParam()
EndMacro

Macro EventInkey()
  Chr( EventwParam() )
EndMacro

Macro EventKeyRepeatCount()
  (EventlParam() & $FFFF)
EndMacro

Macro EventKeyScanCode()
  ((EventlParam() >> 16) & $FF)
EndMacro

Macro EventKeyIsExtended()
  ((EventlParam() >> 24) & $1)
EndMacro

Macro EventKeyPreviousState()
  ((EventlParam() >> 30) & $1)
EndMacro

Macro EventKeyTransitionState()
  ((EventlParam() >> 31) & $1)
EndMacro
Small example:

Code: Select all

If OpenWindow(0, 0, 0, 270, 160, "Keyboard Events", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    
  TextGadget(0, 10, 10, 250, 20, "Last typed character :   ")
  TextGadget(1, 10, 40, 250, 20, "Last pushed key :   ")
  TextGadget(2, 10, 70, 250, 20, "Last released key :   ")
  
  Repeat
    
    Event = WaitWindowEvent()
    
    Select Event
      Case #PB_Event_KeyboardInkey
        String$ = GetGadgetText(0)
        String$ = StringField(String$, 1, #TAB$) + #TAB$ + EventInkey()
        SetGadgetText(0, String$)
        
      Case #PB_Event_KeyboardPushed
        String$ = GetGadgetText(1)
        String$ = StringField(String$, 1, #TAB$) + #TAB$ + Str( EventKey() )
        SetGadgetText(1, String$)
        
      Case #PB_Event_KeyboardReleased
        String$ = GetGadgetText(2)
        String$ = StringField(String$, 1, #TAB$) + #TAB$ + Str( EventKey() )
        SetGadgetText(2, String$)
        
    EndSelect
    
  Until Event = #PB_Event_CloseWindow
EndIf

Dri :D
Last edited by Dr. Dri on Mon Aug 14, 2006 6:25 am, edited 2 times in total.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Excellent! That'll come in very handy, thanks for posting.
BERESHEIT
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

Thank you Dri,

and here is the same but for mouse events :

win32_events.zip

Code: Select all

#WHEEL_DELTA = 120 

Enumeration ; #PB_Event_Mouse 
  #PB_Event_MouseMove              = #WM_MOUSEMOVE 
  #PB_Event_MouseWheel             = #WM_MOUSEWHEEL 
  #PB_Event_MouseLeftUp            = #WM_LBUTTONUP 
  #PB_Event_MouseLeftDown          = #WM_LBUTTONDOWN 
  #PB_Event_MouseLeftDoubleClick   = #WM_LBUTTONDBLCLK 
  #PB_Event_MouseMiddleUp          = #WM_MBUTTONUP 
  #PB_Event_MouseMiddleDown        = #WM_MBUTTONDOWN 
  #PB_Event_MouseMiddleDoubleClick = #WM_MBUTTONDBLCLK 
  #PB_Event_MouseRightUp           = #WM_RBUTTONUP 
  #PB_Event_MouseRightDown         = #WM_RBUTTONDOWN 
  #PB_Event_MouseRightDoubleClick  = #WM_RBUTTONDBLCLK 
EndEnumeration 

Macro EventMouseX() 
  (EventlParam() & $FFFF) 
EndMacro 

Macro EventMouseY() 
  ((EventlParam() >> 16) & $FFFF) 
EndMacro 

Macro EventMouseDelta() 
  ((EventwParam() >> 16) & $FFFF) 
EndMacro 

Macro EventMouseCtrl() 
  ((EventwParam() & $FFFF) & $0008) ; #MK_CONTROL 
EndMacro 

Macro EventMouseShift() 
  ((EventwParam() & $FFFF) & $0004) ; #MK_SHIFT 
EndMacro 

Macro EventMouseLButton() 
  ((EventwParam() & $FFFF) & $0001) ; #MK_LBUTTON 
EndMacro 

Macro EventMouseMButton() 
  ((EventwParam() & $FFFF) & $0010) ; #MK_MBUTTON 
EndMacro 

Macro EventMouseRButton() 
  ((EventwParam() & $FFFF) & $0002) ; #MK_RBUTTON 
EndMacro 

Macro EventMouseXButton1() 
  ((EventwParam() & $FFFF) & $0020) ; #MK_XBUTTON1 (Windows 2000/XP) 
EndMacro 

Macro EventMouseXButton2() 
  ((EventwParam() & $FFFF) & $0040) ; #MK_XBUTTON2 (Windows 2000/XP) 
EndMacro
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
Post Reply