Page 2 of 2

Posted: Tue Sep 25, 2007 11:12 pm
by Flype
my 2 cents :

Code: Select all

Macro MYSTR(x)
  RSet(StrU(x, #Byte), 3, "0")
EndMacro

Dim KeyboardState.b(256)

LoadFont(0, "Courier", 8)

If OpenWindow(0, 0, 0, 570, 255, "GetKeyboardState_()", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  SetWindowColor(0, #White)
  
  Repeat
    
    Select WindowEvent()
      
      Case #PB_Event_CloseWindow
        Break
        
      Case 0
        
        If GetKeyboardState_( @KeyboardState() )
          
          If StartDrawing( WindowOutput(0) )
            DrawingFont( FontID(0) )
            For y = 0 To 15 
              For x = 0 To 15
                Line( 36 * x - 5, 0, 0, 300)
                DrawText( 36 * x, 16 * y, MYSTR( KeyboardState( y << 4 + x) ) )
              Next x 
            Next y 
            StopDrawing()
          EndIf
          
        EndIf
        
        Delay(1)
        
    EndSelect
    
  ForEver
  
EndIf
and please note the Win32 Reference :
When the function returns, each member of the array pointed to by the lpKeyState parameter contains status data for a virtual key. If the high-order bit is 1, the key is down; otherwise, it is up. If the low-order bit is 1, the key is toggled. A key, such as the CAPS LOCK key, is toggled if it is turned on. The key is off and untoggled if the low-order bit is 0. A toggle key's indicator light (if any) on the keyboard will be on when the key is toggled, and off when the key is untoggled.

Posted: Wed Sep 26, 2007 8:41 am
by Shardik
@Kaeru Gaman:
Thank you for your example. But after testing it I at first always received the message "you hit ctrl-a" without having pressed any key... :? Then I read the quote from Flype and improved your example:

Code: Select all

EnableExplicit 

Structure FFmap 
  key.b[256] 
EndStructure 

Define Keys.FFmap 

Define.l EvID 
OpenWindow(0,#PB_Ignore,#PB_Ignore,200,100,"Test for <Ctrl> + <A>") 
CreateGadgetList(WindowID(0)) 
  TextGadget(0,10,10,180,30,"go") 

Repeat 
  EvID = WaitWindowEvent(100) 
  GetKeyboardState_(@Keys)

  If Keys\key[#VK_A] & %10000000 And Keys\key[#VK_CONTROL] & %10000000
    SetGadgetText(0, "you hit ctrl-a") 
  EndIf 
Until EvID = #PB_Event_CloseWindow

Posted: Wed Sep 26, 2007 11:16 am
by Kaeru Gaman
ah thnx.

I admit, my example was a bit "quick'n'dirty"...

Posted: Wed Sep 26, 2007 1:49 pm
by Ollivier
@Shardik

That's the reason Flype checks GetKeyboardState_() in the code above. I wanted to post a code but he is quicker than me...

@kaeru

You forget explaining I bashed you with a dozen menu example ! ! ! :D :D

I'll remember I can't use everytime a direct string pointor (Area.S = Space(256): GetKeyboardState_(@Area) )
I'll remember too, 'EnableExplicit' is a good directive to prevent from bugs.

Thank you again for your coding method.

Posted: Wed Sep 26, 2007 5:25 pm
by TerryHough
Personally, I utilize a much different approach. PB allows me to catch multiple key events such as Ctrl-A or any other I define easily.

Following is an example skeleton program which includes two other pieces of code, also attached. I stripped this out of a working program, so it may contain lots more code than you really need.

Alt-C will call the Windows Calc.exe program
Ctrl-Alt-C will just report the key press
Alt-M will just report the key press
Alt-X will exit the program

Example skeleton program:

Code: Select all

#Window = 0
OpenWindow(#Window,0,0,200,100,"Keyboard Event Example",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
CreateGadgetList(WindowID(#Window))
TextGadget(#PB_Any,10,10,180,30,"Try pressing Alt-C or Ctrl-Alt-C",#PB_Text_Border)
TextGadget(#PB_Any,10,50,180,30,"Try pressing Alt-M or Alt-X",#PB_Text_Border)
While WindowEvent() : Wend

; Define a list of "hot keys" to be monitored by the program using AddKeyboardShortcut.
; These shortcuts generate "menu events" when a defined keyboard shortcut is pressed.
XIncludeFile "Define_HotKeys.pb"

;--Main processing loop
Repeat
  Event      = WaitWindowEvent()
  EventGadget= EventGadget()
  TypeEvent  = EventType()
  
  Select Event
  Case #PB_Event_Menu
    Menu_Event = EventMenu()
    ; Define the event handling for each received "menu event"
    XIncludeFile "Define_Events.pb"
  EndSelect
  ; Example - pressing a two key "hot key" Alt-X will set the variable Quit = #True
  ; which will force the program to end.
Until Quit = #True Or Event = #PB_Event_CloseWindow

End
Hot key definition code (define as many or as few as you like):
Save this as Define_HotKeys.pb

Code: Select all

; CapturingKeys - TerryHough

Enumeration 700
#HK_Alt_1
#HK_Alt_3
#HK_Alt_0
#HK_Alt_B
#HK_Alt_C
#HK_Alt_D
#HK_Alt_E
#HK_Alt_G
#HK_Alt_L
#HK_Alt_M
#HK_Alt_N
#HK_Alt_O
#HK_Alt_P
#HK_Alt_S
#HK_Alt_T
#HK_Alt_X
#HK_Alt_Z
#HK_Alt_F2
#HK_Alt_Equal
#HK_Alt_PgUp
#HK_Alt_PgDn

#HK_Esc
#HK_PgUp
#HK_PgDn
#HK_Home
#HK_End
#HK_Up
#HK_Down
#HK_Left
#HK_Right
#HK_F1
#HK_F2
#HK_F5
#HK_F6
#HK_F7
#HK_F9
#HK_F10
#HK_Return

#HK_Ctrl_E
#HK_Ctrl_F
#HK_Ctrl_P
#HK_Ctrl_F6
#HK_Ctrl_F9
#HK_Ctrl_F10
#HK_Ctrl_Left
#HK_Ctrl_Right

#HK_Shift_Left
#HK_Shift_Right
#HK_Shift_Up
#HK_Shift_Down

#HK_Ctrl_Alt_C
EndEnumeration

AddKeyboardShortcut(#Window, #PB_Shortcut_Return, #HK_Return)
AddKeyboardShortcut(#Window, #PB_Shortcut_Escape, #HK_Esc)
AddKeyboardShortcut(#Window, #PB_Shortcut_Alt | #PB_Shortcut_1 , #HK_Alt_1)
AddKeyboardShortcut(#Window, #PB_Shortcut_Alt | #PB_Shortcut_3 , #HK_Alt_3)
AddKeyboardShortcut(#Window, #PB_Shortcut_Alt | #PB_Shortcut_0 , #HK_Alt_0)
AddKeyboardShortcut(#Window, #PB_Shortcut_Alt | #PB_Shortcut_B , #HK_Alt_B)
AddKeyboardShortcut(#Window, #PB_Shortcut_Alt | #PB_Shortcut_C , #HK_Alt_C)
AddKeyboardShortcut(#Window, #PB_Shortcut_Alt | #PB_Shortcut_D , #HK_Alt_D)
AddKeyboardShortcut(#Window, #PB_Shortcut_Alt | #PB_Shortcut_E , #HK_Alt_E)
AddKeyboardShortcut(#Window, #PB_Shortcut_Alt | #PB_Shortcut_G , #HK_Alt_G)
AddKeyboardShortcut(#Window, #PB_Shortcut_Alt | #PB_Shortcut_L , #HK_Alt_L)
AddKeyboardShortcut(#Window, #PB_Shortcut_Alt | #PB_Shortcut_M , #HK_Alt_M)
AddKeyboardShortcut(#Window, #PB_Shortcut_Alt | #PB_Shortcut_N , #HK_Alt_N)
AddKeyboardShortcut(#Window, #PB_Shortcut_Alt | #PB_Shortcut_O , #HK_Alt_O)
AddKeyboardShortcut(#Window, #PB_Shortcut_Alt | #PB_Shortcut_P , #HK_Alt_P)
AddKeyboardShortcut(#Window, #PB_Shortcut_Alt | #PB_Shortcut_S , #HK_Alt_S)
AddKeyboardShortcut(#Window, #PB_Shortcut_Alt | #PB_Shortcut_T , #HK_Alt_T)
AddKeyboardShortcut(#Window, #PB_Shortcut_Alt | #PB_Shortcut_X , #HK_Alt_X)
AddKeyboardShortcut(#Window, #PB_Shortcut_Alt | #PB_Shortcut_Z , #HK_Alt_Z)
AddKeyboardShortcut(#Window, #PB_Shortcut_Alt | #PB_Shortcut_F2, #HK_Alt_F2)
AddKeyboardShortcut(#Window, #PB_Shortcut_Alt | #PB_Shortcut_Prior , #HK_Alt_PgUp)
AddKeyboardShortcut(#Window, #PB_Shortcut_Alt | #PB_Shortcut_Next , #HK_Alt_PgDn)
AddKeyboardShortcut(#Window, #PB_Shortcut_Prior , #HK_PgUp)
AddKeyboardShortcut(#Window, #PB_Shortcut_Next  , #HK_PgDn)
AddKeyboardShortcut(#Window, #PB_Shortcut_Home , #HK_Home)
AddKeyboardShortcut(#Window, #PB_Shortcut_End  , #HK_End)
AddKeyboardShortcut(#Window, #PB_Shortcut_Up   , #HK_Up)
AddKeyboardShortcut(#Window, #PB_Shortcut_Down , #HK_Down)
AddKeyboardShortcut(#Window, #PB_Shortcut_Left , #HK_Left)
AddKeyboardShortcut(#Window, #PB_Shortcut_Right, #HK_Right)
AddKeyboardShortcut(#Window, #PB_Shortcut_F1   , #HK_F1)
AddKeyboardShortcut(#Window, #PB_Shortcut_F2   , #HK_F2)
AddKeyboardShortcut(#Window, #PB_Shortcut_F5   , #HK_F5)
AddKeyboardShortcut(#Window, #PB_Shortcut_F6   , #HK_F6)
AddKeyboardShortcut(#Window, #PB_Shortcut_F7   , #HK_F7)
AddKeyboardShortcut(#Window, #PB_Shortcut_F9   , #HK_F9)
AddKeyboardShortcut(#Window, #PB_Shortcut_F10  , #HK_F10)
AddKeyboardShortcut(#Window, #PB_Shortcut_Control | #PB_Shortcut_E   , #HK_Ctrl_E)
AddKeyboardShortcut(#Window, #PB_Shortcut_Control | #PB_Shortcut_F   , #HK_Ctrl_F)
AddKeyboardShortcut(#Window, #PB_Shortcut_Control | #PB_Shortcut_P   , #HK_Ctrl_P)
AddKeyboardShortcut(#Window, #PB_Shortcut_Control | #PB_Shortcut_F6  , #HK_Ctrl_F6)
AddKeyboardShortcut(#Window, #PB_Shortcut_Control | #PB_Shortcut_F9  , #HK_Ctrl_F9)
AddKeyboardShortcut(#Window, #PB_Shortcut_Control | #PB_Shortcut_F10 , #HK_Ctrl_F10)
AddKeyboardShortcut(#Window, #PB_Shortcut_Control | #PB_Shortcut_Alt | #PB_Shortcut_C , #HK_Ctrl_Alt_C)
AddKeyboardShortcut(#Window, #PB_Shortcut_Control | #PB_Shortcut_Left , #HK_Ctrl_Left)
AddKeyboardShortcut(#Window, #PB_Shortcut_Control | #PB_Shortcut_Right, #HK_Ctrl_Right)
AddKeyboardShortcut(#Window, #PB_Shortcut_Shift   | #PB_Shortcut_Left , #HK_Shift_Left)
AddKeyboardShortcut(#Window, #PB_Shortcut_Shift   | #PB_Shortcut_Right, #HK_Shift_Right)
AddKeyboardShortcut(#Window, #PB_Shortcut_Shift   | #PB_Shortcut_Up,    #HK_Shift_Up)
AddKeyboardShortcut(#Window, #PB_Shortcut_Shift   | #PB_Shortcut_Down,  #HK_Shift_Down)
Defining the related events for each defined hot key:
Save this as Define_Events.pb

Code: Select all

;-------------------------------- DEFINE_EVENTS -------------------------------
;  Defines the Menu events.
; -----------------------------------------------------------------------------
Select Menu_Event ; Main menu events

;-->Hot key events
Case #HK_Esc
Case #HK_Return
Case #HK_Ctrl_E
Case #HK_Ctrl_P
Case #HK_Alt_F2
Case #HK_Alt_1
Case #HK_Alt_3
Case #HK_Alt_0
Case #HK_Alt_B
Case #HK_Alt_C ; Calculator
  RunProgram("Calc.exe","","")
  Delay(1000)
Case #HK_Alt_D
Case #HK_Alt_E
Case #HK_Alt_G
Case #HK_Alt_L
Case #HK_Alt_M
  MessageRequester("Keypress received","Alt-M",#MB_ICONINFORMATION)
Case #HK_Alt_N
Case #HK_Alt_O
Case #HK_Alt_P
Case #HK_Alt_S
Case #HK_Alt_T
Case #HK_Alt_X
  Quit=#True ; Set to Quit the software pkg
Case #HK_Alt_Z
Case #HK_Alt_PgUp
Case #HK_Alt_PgDn
Case #HK_PgUp
Case #HK_PgDn
Case #HK_Home
Case #HK_End
Case #HK_Up
Case #HK_Down
Case #HK_Left
Case #HK_Right
Case #HK_F1
Case #HK_F2
Case #HK_F5
Case #HK_F6
Case #HK_F7
Case #HK_F9
Case #HK_F10
Case #HK_Ctrl_Alt_C
  MessageRequester("Keypress received","Ctrl-Alt-C",#MB_ICONINFORMATION)
Case #HK_Ctrl_F6
Case #HK_Ctrl_F9
Case #HK_Ctrl_F10
Case #HK_Ctrl_Left
Case #HK_Ctrl_Right
Case #HK_Shift_Left
Case #HK_Shift_Right
Case #HK_Shift_Up
Case #HK_Shift_Down
EndSelect