Page 1 of 1

Get dead keys in canvases?

Posted: Thu Feb 08, 2024 9:12 pm
by Joubarbe

Code: Select all


Procedure OnKeyDown()
  Define char = GetGadgetAttribute(0, #PB_Canvas_Key)
  
  Debug "Raw key: " + char
EndProcedure

Procedure OnInput()
  Define char$ = Chr(GetGadgetAttribute(0, #PB_Canvas_Input)) 
  
  Debug Bool(GetGadgetAttribute(0, #PB_Canvas_Modifiers) = #PB_Canvas_Shift) ; Only works with #PB_Canvas_Shift.
  Debug "Input: " + char$
EndProcedure

OpenWindow(0, 0, 0, 500, 500, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
CanvasGadget(0, 0, 0, 500, 500, #PB_Canvas_Keyboard)
SetActiveGadget(0)

BindGadgetEvent(0, @OnKeyDown(), #PB_EventType_KeyDown)
BindGadgetEvent(0, @OnInput(), #PB_EventType_Input)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
I'm trying to get TAB, ALT, SHIFT+TAB and CTRL+F. I'm on Windows 11 with PB 6.04. What am I doing wrong? :)

Re: Get dead keys in canvases?

Posted: Fri Feb 09, 2024 7:07 am
by wombats
An option for the Tab key is to use AddKeyboardShortcut. I think Window's default tab handling might prevent the CanvasGadget from detecting it.

#PB_Canvas_Modifiers returns a bitwise value, so it might not be exactly equal to #PB_Canvas_Shift or #PB_Canvas_Control. Use the bitwise AND (&) operator to check if one is held down. I'm unsure why Control doesn't seem to work with #PB_Canvas_Input.

Code: Select all

Procedure OnGainFocus()
  AddKeyboardShortcut(0, #PB_Shortcut_Tab, 0)
EndProcedure

Procedure OnLostFocus()
  RemoveKeyboardShortcut(0, #PB_Shortcut_Tab)
EndProcedure

Procedure OnMenuEvent()
  Select EventMenu()
    Case 0
      Debug "Tab"
  EndSelect
EndProcedure

Procedure OnKeyDown()
  Define char = GetGadgetAttribute(0, #PB_Canvas_Key)
  If GetGadgetAttribute(0, #PB_Canvas_Modifiers) & #PB_Canvas_Shift
    Debug "Key Down Shift"
  EndIf
  
  If GetGadgetAttribute(0, #PB_Canvas_Modifiers) & #PB_Canvas_Control
    Debug "Key Down Control"
  EndIf
  Debug "Raw key: " + char
EndProcedure

Procedure OnInput()
  Define char$ = Chr(GetGadgetAttribute(0, #PB_Canvas_Input))
  
  If GetGadgetAttribute(0, #PB_Canvas_Modifiers) & #PB_Canvas_Shift
    Debug "Input Shift"
  EndIf
  
  If GetGadgetAttribute(0, #PB_Canvas_Modifiers) & #PB_Canvas_Control
    Debug "Input Control"
  EndIf

  Debug "Input: " + char$
EndProcedure

OpenWindow(0, 0, 0, 500, 500, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
CanvasGadget(0, 0, 0, 500, 500, #PB_Canvas_Keyboard | #PB_Canvas_DrawFocus)
SetActiveGadget(0)

BindGadgetEvent(0, @OnKeyDown(), #PB_EventType_KeyDown)
BindGadgetEvent(0, @OnInput(), #PB_EventType_Input)
BindGadgetEvent(0, @OnGainFocus(), #PB_EventType_Focus)
BindGadgetEvent(0, @OnGainFocus(), #PB_EventType_LostFocus)
BindEvent(#PB_Event_Menu, @OnMenuEvent())

PostEvent(#PB_Event_Gadget, 0, 0, #PB_EventType_Focus)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow

Re: Get dead keys in canvases?

Posted: Fri Feb 09, 2024 9:15 am
by Joubarbe
Thanks for your code.

ALT seems to have weird behaviour, once you press it once, SHIFT and CONTROL seem to not work anymore (lost of focus?).

Code: Select all

Procedure OnGainFocus()
  AddKeyboardShortcut(0, #PB_Shortcut_Tab, 0)
  AddKeyboardShortcut(0, #PB_Shortcut_Alt, 1)
EndProcedure

Procedure OnLostFocus()
  RemoveKeyboardShortcut(0, #PB_Shortcut_Tab)
  RemoveKeyboardShortcut(0, #PB_Shortcut_Alt)
EndProcedure

Procedure OnMenuEvent()
  Select EventMenu()
    Case 0
      If GetGadgetAttribute(0, #PB_Canvas_Modifiers) & #PB_Canvas_Shift
        Debug "Tab + SHIFT"
      Else
        Debug "Tab"
      EndIf
    Case 1
      Debug "Alt"
  EndSelect
EndProcedure
I've tried your TAB solution for ALT, but it's not recognised. SHIFT + TAB doesn't work either. Should it be considered a bug? This doesn't seem a normal behaviour.

Re: Get dead keys in canvases?

Posted: Fri Feb 09, 2024 8:20 pm
by skywalk
I don't understand your requirement?
I use dozens of keyboard shortcuts with the canvasgadget.

Code: Select all

#EV_KS_CTRL_T = 100
#EV_KS_ALT_T  = 101
#EV_KS_TAB    = 102
OpenWindow(0, 0, 0, 500, 500, "Canvas Keyboard Shortcuts", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
CanvasGadget(0, 0, 0, 500, 500, #PB_Canvas_Keyboard | #PB_Canvas_DrawFocus)
SetActiveGadget(0)
AddKeyboardShortcut(0, #PB_Shortcut_Control | #PB_Shortcut_T, #EV_KS_CTRL_T)
AddKeyboardShortcut(0, #PB_Shortcut_Alt | #PB_Shortcut_T, #EV_KS_ALT_T)
AddKeyboardShortcut(0, #PB_Shortcut_Tab, #EV_KS_TAB)
Repeat
  evM = EventMenu()
  Select evM
  Case #EV_KS_TAB
    MessageRequester("Canvas Keyboard Shortcuts","TAB")
  Case #EV_KS_CTRL_T
    MessageRequester("Canvas Keyboard Shortcuts","CTRL+T")
  Case #EV_KS_ALT_T
    MessageRequester("Canvas Keyboard Shortcuts","ALT+T")
  EndSelect
Until WaitWindowEvent() = #PB_Event_CloseWindow

Re: Get dead keys in canvases?

Posted: Tue Feb 13, 2024 11:20 am
by Joubarbe
Thanks for your help. I want to detect CTRL and ALT on their own. It doesn't work with the input event, nor key down, and doesn't work with the AddKeyboardShortcut() method. Besides, GetAttributeModifier() cannot be used to detect those keys, unlike what the documentation says about #PB_Canvas_Modifiers in the Canvas Gadget help section (as far as I know and experienced, and I might be wrong).

Re: Get dead keys in canvases?

Posted: Thu Feb 15, 2024 4:00 pm
by skywalk
[ctrl] and [alt] have no real action by themselves so what are you trying to accomplish?
Here is mousemove + [ctrl] or [alt] on the canvasgadget.

Code: Select all

#CG = 0
#EV_KS_CTRL_T = 100
#EV_KS_ALT_T  = 101
#EV_KS_TAB    = 102
OpenWindow(0, 0, 0, 500, 500, "Canvas Keyboard Shortcuts", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
CanvasGadget(#CG, 0, 0, 500, 500, #PB_Canvas_Keyboard | #PB_Canvas_DrawFocus)
SetActiveGadget(#CG)
AddKeyboardShortcut(0, #PB_Shortcut_Control | #PB_Shortcut_T, #EV_KS_CTRL_T)
AddKeyboardShortcut(0, #PB_Shortcut_Alt | #PB_Shortcut_T, #EV_KS_ALT_T)
AddKeyboardShortcut(0, #PB_Shortcut_Tab, #EV_KS_TAB)
Repeat
  evWW = WaitWindowEvent(10)
  Select evWW
  Case #PB_Event_CloseWindow
    Break
  Case #PB_Event_Menu
    evM = EventMenu()
    Select evM
    Case #EV_KS_TAB
      MessageRequester("Canvas Keyboard Shortcuts","TAB")
    Case #EV_KS_CTRL_T
      MessageRequester("Canvas Keyboard Shortcuts","CTRL+T")
    Case #EV_KS_ALT_T
      MessageRequester("Canvas Keyboard Shortcuts","ALT+T")
    EndSelect
  Case #PB_Event_Gadget
    evG = EventGadget()
    If evG = #CG
      evT = EventType()
      Select evT
      Case #PB_EventType_MouseMove
        If GetGadgetAttribute(#CG, #PB_Canvas_Modifiers) & #PB_Canvas_Alt
          Debug "Alt+MouseMove = " + Str(GetGadgetAttribute(#CG, #PB_Canvas_MouseX)) + "," + Str(GetGadgetAttribute(#CG, #PB_Canvas_MouseY))
        ElseIf GetGadgetAttribute(#CG, #PB_Canvas_Modifiers) & #PB_Canvas_Control
          Debug "Ctrl+MouseMove = " + Str(GetGadgetAttribute(#CG, #PB_Canvas_MouseX)) + "," + Str(GetGadgetAttribute(#CG, #PB_Canvas_MouseY))
        EndIf
      EndSelect
    EndIf
  EndSelect
ForEver

Re: Get dead keys in canvases?

Posted: Mon Feb 19, 2024 4:00 am
by Joubarbe
I have a custom GUI, and a menu bar. Pressing ALT on its own would make the menu active, so you can navigate through it, like it does on Windows.