Page 1 of 1

Window with keyboard shortcut for control key recognition.

Posted: Wed Sep 28, 2022 2:42 pm
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

Re: Window with keyboard shortcut for control key recognition.

Posted: Wed Sep 28, 2022 5:06 pm
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.

Re: Window with keyboard shortcut for control key recognition.

Posted: Wed Sep 28, 2022 7:03 pm
by bmon
That does work Axolotl ... Thank you my friend!