Shortcut Key That Works Globally

[quote=185995:@jim mckay]Ok, since I suggested it…

You could either use a module or a class (slightly more complicated)

in a module…

add a property

Hook as integer

add a structure

  keyCode as uint32
  scanCode as uint32
  flags as uint32
  time as uint32
  extraInfo as ptr

and 4 methods

[code]Sub Start()
#if TargetWin32

soft Declare function SetWindowsHookExA lib "User32" (idHook as integer, lpfn as ptr, hMod as integer,dwThreadId as integer) as integer
if hook<>0 then return
hook= SetWindowsHookExA(WH_KEYBOARD_LL,AddressOf KeyCallback,0,0)

End Sub

Function HandleKeyPress(keyCode as integer,down as Boolean) As Boolean
Return false
End Function

Function KeyCallBack(code as int32, wParam as uint32, byref lParam as KBDLLHOOKSTRUCT) As int32
#if TargetWin32
const WM_KEYDOWN =&h100
soft Declare function CallNextHookEx lib “User32” (hhk as integer,nCode as int32,wParam as uint32,byref lParam as KBDLLHOOKSTRUCT) as int32
if code<0 or (not HandleKeyPress(lParam.keyCode,wParam=WM_KEYDOWN)) then Return CallNextHookEx(0,code,wParam,lParam)
Return -1
End Function

Sub Stop()
#if TargetWin32

soft Declare function UnhookWindowsHookEx lib "User32" (idHook as integer) as integer
call UnhookWindowsHookEx(hook)

End Sub

To start monitoring key events call


To stop call


now, in the handleKeyPress method, you can return true to prevent the event from moving up the call chain by returning true.

if keyboard.AsyncControlKey and keyCode=&h12 then //do stuff return true //prevents "a" from being typed.. should also supersede any menus end if[/quote]
Thankyou very much for the suggestion. I already came up with my own code that works cross platform to handle this.


[quote=186036:@Will Shank]Yoou can programmatically edit MenuBars. So create a method that’ll add the menuitems you want to be on all menubars, then in App.Open call it with each menubar.

[code]Sub Open() //App


End Sub

Private Sub addGlobalMenuItems(mb As MenuBar)

dim m As MenuItem

m = new MenuItem(“Test1”)
m.KeyboardShortcut = “Shift-Cmd-P”
m.Enabled = true
AddHandler m.Action, AddressOf App.handleTest1
mb.Child(“FileMenu”).Append m

m = new MenuItem(“Test2”)
m.KeyboardShortcut = “Shift-Cmd-L”
m.Enabled = true
AddHandler m.Action, AddressOf App.handleTest2
mb.Child(“FileMenu”).Append m

End Sub

Private Function handleTest1(sender As MenuItem) As Boolean
MsgBox “triggered App.handleTest1”
End Function

Private Function handleTest2(sender As MenuItem) As Boolean
MsgBox “triggered App.handleTest2”
End Function[/code]

This works on Mac, for Windows change “Cmd” to “Cntl”. see[/quote]
Thanks for the suggestion.

I have written a class that allows you to detect the presses of shortcut keys at any given time. It seems to work really well! :slight_smile:

Thanks for the suggestions anyway.