AddKeyboardShortcut() and keyboard layouts

Mac OSX specific forum
User avatar
deseven
Enthusiast
Enthusiast
Posts: 296
Joined: Wed Jan 12, 2011 3:48 pm
Location: Serbia
Contact:

AddKeyboardShortcut() and keyboard layouts

Post by deseven »

Check out this simple example:

Code: Select all

OpenWindow(0,0,0,400,300,"shortcut test",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)

AddKeyboardShortcut(0,#PB_Shortcut_Command|#PB_Shortcut_F,0)

Repeat
  ev = WaitWindowEvent()
  If ev = #PB_Event_Menu And EventMenu() = 0
    Debug "shortcut"
  EndIf
Until ev = #PB_Event_CloseWindow
This correctly works if I press cmd+f with English keyboard layout, but doesn't work in any other layout (no event is firing). I checked PB IDE and all shortcuts there work independently of currently active layout, so I tried to check IDE's sources, but haven't found anything specific, looks like it simply uses AddKeyboardShortcut() like I do.

I'm sure this is something minor and can be easily fixed, but have no idea where to look, can someone help?
hoerbie
User
User
Posts: 71
Joined: Fri Dec 06, 2013 11:57 am
Location: DE/BY/MUC

Re: AddKeyboardShortcut() and keyboard layouts

Post by hoerbie »

Checked it on my Intel MBP 13" (2020) with Monterey 12.0.1 and its german keyboard layout, the shortcut works.

Just an idea, as I remember similar problem from anywhere else: Did you try a number <> 0 for the fired EventMenu() value?
User avatar
deseven
Enthusiast
Enthusiast
Posts: 296
Joined: Wed Jan 12, 2011 3:48 pm
Location: Serbia
Contact:

Re: AddKeyboardShortcut() and keyboard layouts

Post by deseven »

Tried different numbers, no luck.

Any chance you can try adding non-latin layouts? Like Russian or maybe Greek.
User avatar
deseven
Enthusiast
Enthusiast
Posts: 296
Joined: Wed Jan 12, 2011 3:48 pm
Location: Serbia
Contact:

Re: AddKeyboardShortcut() and keyboard layouts

Post by deseven »

Ok, I browsed the IDE's code some more and found this:

Code: Select all

CompilerIf #CompileMac
    If Shortcut & #PB_Shortcut_Command
      Text$ + "+CMD" ; do not use the name from the Language here, as only "CMD" produces that key symbol
    EndIf
  CompilerEndIf
  
  CompilerIf #CompileWindows = 0
    
    ; On Linux and OSX, the MenuItem command parses the text for shortcuts, so it needs to be the
    ; english name here!
    ;
    If Shortcut & #PB_Shortcut_Control
      Text$ + "+Ctrl"
    EndIf
    If Shortcut & #PB_Shortcut_Alt
      Text$ + "+Alt"
    EndIf
    If Shortcut & #PB_Shortcut_Shift
      Text$ + "+Shift"
    EndIf
    
  CompilerElse
    ...
So this works just fine in all layouts:

Code: Select all

OpenWindow(0,0,0,400,300,"shortcut test",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)

CreateMenu(0,WindowID(0))
MenuTitle("test")
MenuItem(0,"test" + Chr(9) + "+CMD+F")

; not even needed if we have a menu
;AddKeyboardShortcut(0,#PB_Shortcut_Command|#PB_Shortcut_F,100)

Repeat
  ev = WaitWindowEvent()
  If ev = #PB_Event_Menu And EventMenu() = 0
    Debug "shortcut"
  EndIf
Until ev = #PB_Event_CloseWindow
Still, what if I don't want a menu?
Post Reply