Window with keyboard shortcut for control key recognition.

Just starting out? Need help? Post your questions and find answers here.
bmon
User
User
Posts: 54
Joined: Sat May 24, 2008 8:51 pm
Location: U.S.

Window with keyboard shortcut for control key recognition.

Post by bmon »

Hello ...
Need a little help in recognizing when just the control key is being pressed from inside a created window. Control key in combination with another key is working okay. Much appreciated for the help!

Code: Select all

EnableExplicit

Global gEvent.i, gDone.i = 0

Enumeration 1
  #KEYBOARD_CONTROL
  #KEYBOARD_Q_CONTROL
  #KEYBOARD_ESC
EndEnumeration

Debug "Hello!"

If OpenWindow(1,0,0,1024,768,"",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  AddKeyboardShortcut(1,#PB_Shortcut_Control, #KEYBOARD_CONTROL)
  AddKeyboardShortcut(1,#PB_Shortcut_Q | #PB_Shortcut_Control, #KEYBOARD_Q_CONTROL)
  AddKeyboardShortcut(1,#PB_Shortcut_Escape, #KEYBOARD_ESC)
  
  Repeat
    gEvent = WaitWindowEvent(5)
    If gEvent = #PB_Event_CloseWindow
      gDone = 1
    EndIf
    
    If gEvent = #PB_Event_Menu
      If EventMenu() = #KEYBOARD_Q_CONTROL
        Debug "Q + CONTROL"
      EndIf
      If EventMenu() = #KEYBOARD_CONTROL
        Debug "CONTROL"
      EndIf
      If EventMenu() = #KEYBOARD_ESC
        gDone = 1
      EndIf
    EndIf
  Until gDone = 1
EndIf

If IsWindow(1)
  CloseWindow(1)
EndIf

End
Thanks again ...
Bruce
Axolotl
Enthusiast
Enthusiast
Posts: 435
Joined: Wed Dec 31, 2008 3:36 pm

Re: Window with keyboard shortcut for control key recognition.

Post by Axolotl »

Hi bmon,

on windows (only) you can use this little api function to get kind of a result.
Maybe this is what you are looking for.

Code: Select all

EnableExplicit

Global gEvent.i, gDone.i = 0 

Enumeration 1
  #KEYBOARD_CONTROL
  #KEYBOARD_Q_CONTROL
  #KEYBOARD_ESC
EndEnumeration

Debug "Hello!"

If OpenWindow(1,0,0,1024,768,"",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  AddKeyboardShortcut(1,#PB_Shortcut_Control, #KEYBOARD_CONTROL)
  AddKeyboardShortcut(1,#PB_Shortcut_Q | #PB_Shortcut_Control, #KEYBOARD_Q_CONTROL)
  AddKeyboardShortcut(1,#PB_Shortcut_Escape, #KEYBOARD_ESC)
  
  Repeat
    gEvent = WaitWindowEvent(5)
    If gEvent = #PB_Event_CloseWindow
      gDone = 1
    EndIf
    
    If gEvent = #PB_Event_Menu
      If EventMenu() = #KEYBOARD_Q_CONTROL
        Debug "Q + CONTROL"
      EndIf
      If EventMenu() = #KEYBOARD_CONTROL
        Debug "CONTROL"
      EndIf
      If EventMenu() = #KEYBOARD_ESC
        gDone = 1
      EndIf
    EndIf

  ; ; add this little if-endif-block here to catch the control key 
    If GetAsyncKeyState_(#VK_CONTROL) & $0001 ; <==> key was pressed after the previous call to GetAsyncKeyState_() 
      Debug "CONTROL"
    EndIf

  Until gDone = 1
EndIf

If IsWindow(1)
  CloseWindow(1)
EndIf

End 
Happy coding and stay healthy.
Mostly running PureBasic <latest stable version and current alpha/beta> (x64) on Windows 11 Home
bmon
User
User
Posts: 54
Joined: Sat May 24, 2008 8:51 pm
Location: U.S.

Re: Window with keyboard shortcut for control key recognition.

Post by bmon »

That does work Axolotl ... Thank you my friend!
Post Reply