Page 3 of 3
Re: Global keyboard hook
Posted: Mon Aug 24, 2020 12:36 pm
by jvzuck
Argh! CGEventTapCreate is working perfectly when I run the program from within the IDE but it's returning a NULL (ie not creating the tap) when I tried to run as a complied executable. My app has permissions but still no tap is being created. There doesn't appear to be a built in error handler for this function. It just either returns an eventtap or doesn't and now it won't. Anyone tried to create a global hotkey program and then distribute it?!
Jonathan
Re: Global keyboard hook
Posted: Mon Aug 24, 2020 2:27 pm
by deseven
I'm not sure if it was covered somewhere. App bundles is just a layer of abstraction, they are basically just folders, you can right click on any app and select "Show Package Contents".
You can use "Create executable" from the IDE itself, but you will have to edit your Info.plist manually after that.
You can look into
my shortcut manager and
hotkey module it uses (it has an example).
Re: Global keyboard hook
Posted: Mon Aug 24, 2020 6:38 pm
by jvzuck
Thanks! Lots of interesting code to study there! I see that you're using a different technique to capture keys. I'm using an event tap because i'm trying to capture the numeric keypad as separate from the main number keys so I still have miles to go before I sleep, apparently. It makes no sense to me that it works perfectly in the IDE and then suddenly fails when I create an executable. Sigh.
Re: Global keyboard hook
Posted: Tue Aug 25, 2020 3:52 am
by deseven
Ah, sorry, i forgot, there is a description key that is required from 10.14 i think, it's called NSAppleEventsUsageDescription.
1. "Create executable" from vwidmer's example.
2. Edit Contents/Info.plist inside.
3. Add
Code: Select all
<key>CFBundleIdentifier</key>
<string>com.example.myapp</string>
<key>NSAppleEventsUsageDescription</key>
<string>Access to system events is required to handle keyboard shortcuts.</string>
4. Rename app bundle to something else (like add '2' after program name or something, that's needed for system to re-read Info.plist).
5. Go to system settings, Security and Privacy, add your app inside Accessibility section.
6. Run the app as usual.
As for "why it works in IDE"... As i said, PB doesn't support app bundles correctly, it runs newly created app like a common unix executable and macos consider it to be a part of the parent app (in this case PB IDE itself). At least that's my interpretation of things.
Re: Global keyboard hook
Posted: Tue Aug 25, 2020 5:13 am
by jvzuck
So, this did NOT work for me, either by running as admin OR by editing plist. There's something strange that happens when doing an event tap that's not working in a standalone executable. $5 to whomever solves it!
Jonathan
vwidmer wrote:So I got it working with admin permissions no need to do anything in Accessibility.
I didnt try to modify anything in the plist or anything so maybe it is possible to get it to run as user?
Here is my slightly modified version..
Code: Select all
EnableExplicit
#KeyDownMask = 1 << 10
#KeyUpMask = 1 << 11
#FlagsChangedMask = 1 << 12
;#kCGSessionEventTap = 0
#kCGHeadInsertEventTap = 0
#kCGKeyboardEventKeycode = 9
ImportC ""
CGEventTapCreate(tap, place, options, eventsOfInterest.q, callback, refcon)
CGEventGetFlags.q(event)
CGEventGetIntegerValueField.q(event, field)
CGEventKeyboardGetUnicodeString(event, maxStringLength, *actualStringLength, *unicodeString)
EndImport
Define eventTap
Define eventMask
Procedure.s convertKeyCode(keyCode)
Select keyCode
Case 0: pReturn$ = "a"
Case 1: pReturn$ = "s"
Case 2: pReturn$ = "d"
Case 3: pReturn$ = "f"
Case 4: pReturn$ = "h"
Case 5: pReturn$ = "g"
Case 6: pReturn$ = "z"
Case 7: pReturn$ = "x"
Case 8: pReturn$ = "c"
Case 9: pReturn$ = "v"
Case 11: pReturn$ = "b"
Case 12: pReturn$ = "q"
Case 13: pReturn$ = "w"
Case 14: pReturn$ = "e"
Case 15: pReturn$ = "r"
Case 16: pReturn$ = "y"
Case 17: pReturn$ = "t"
Case 18: pReturn$ = "1"
Case 19: pReturn$ = "2"
Case 20: pReturn$ = "3"
Case 21: pReturn$ = "4"
Case 22: pReturn$ = "6"
Case 23: pReturn$ = "5"
Case 24: pReturn$ = "="
Case 25: pReturn$ = "9"
Case 26: pReturn$ = "7"
Case 27: pReturn$ = "-"
Case 28: pReturn$ = "8"
Case 29: pReturn$ = "0"
Case 30: pReturn$ = "]"
Case 31: pReturn$ = "o"
Case 32: pReturn$ = "u"
Case 33: pReturn$ = "["
Case 34: pReturn$ = "i"
Case 35: pReturn$ = "p"
Case 37: pReturn$ = "l"
Case 38: pReturn$ = "j"
Case 39: pReturn$ = "'"
Case 40: pReturn$ = "k"
Case 41: pReturn$ = ";"
Case 42: pReturn$ = "\\"
Case 43: pReturn$ = ","
Case 44: pReturn$ = "/"
Case 45: pReturn$ = "n"
Case 46: pReturn$ = "m"
Case 47: pReturn$ = "."
Case 50: pReturn$ = "`"
Case 65: pReturn$ = "[decimal]"
Case 67: pReturn$ = "[asterisk]"
Case 69: pReturn$ = "[plus]"
Case 71: pReturn$ = "[clear]"
Case 75: pReturn$ = "[divide]"
Case 76: pReturn$ = "[enter]"
Case 78: pReturn$ = "[hyphen]"
Case 81: pReturn$ = "[equals]"
Case 82: pReturn$ = "0"
Case 83: pReturn$ = "1"
Case 84: pReturn$ = "2"
Case 85: pReturn$ = "3"
Case 86: pReturn$ = "4"
Case 87: pReturn$ = "5"
Case 88: pReturn$ = "6"
Case 89: pReturn$ = "7"
Case 91: pReturn$ = "8"
Case 92: pReturn$ = "9"
Case 36: pReturn$ = "[return]"
Case 48: pReturn$ = "[tab]"
Case 49: pReturn$ = " "
Case 51: pReturn$ = "[del]"
Case 53: pReturn$ = "[esc]"
Case 54: pReturn$ = "[right-cmd]"
Case 55: pReturn$ = "[left-cmd]"
Case 56: pReturn$ = "[left-shift]"
Case 57: pReturn$ = "[caps]"
Case 58: pReturn$ = "[left-option]"
Case 59: pReturn$ = "[left-ctrl]"
Case 60: pReturn$ = "[right-shift]"
Case 61: pReturn$ = "[right-option]"
Case 62: pReturn$ = "[right-ctrl]"
Case 63: pReturn$ = "[fn]"
Case 64: pReturn$ = "[f17]"
Case 72: pReturn$ = "[volup]"
Case 73: pReturn$ = "[voldown]"
Case 74: pReturn$ = "[mute]"
Case 79: pReturn$ = "[f18]"
Case 80: pReturn$ = "[f19]"
Case 90: pReturn$ = "[f20]"
Case 96: pReturn$ = "[f5]"
Case 97: pReturn$ = "[f6]"
Case 98: pReturn$ = "[f7]"
Case 99: pReturn$ = "[f3]"
Case 100: pReturn$ = "[f8]"
Case 101: pReturn$ = "[f9]"
Case 103: pReturn$ = "[f11]"
Case 105: pReturn$ = "[f13]"
Case 106: pReturn$ = "[f16]"
Case 107: pReturn$ = "[f14]"
Case 109: pReturn$ = "[f10]"
Case 111: pReturn$ = "[f12]"
Case 113: pReturn$ = "[f15]"
Case 114: pReturn$ = "[help]"
Case 115: pReturn$ = "[home]"
Case 116: pReturn$ = "[pgup]"
Case 117: pReturn$ = "[fwddel]"
Case 118: pReturn$ = "[f4]"
Case 119: pReturn$ = "[end]"
Case 120: pReturn$ = "[f2]"
Case 121: pReturn$ = "[pgdown]"
Case 122: pReturn$ = "[f1]"
Case 123: pReturn$ = "[left]"
Case 124: pReturn$ = "[right]"
Case 125: pReturn$ = "[down]"
Case 126: pReturn$ = "[up]"
Default
pReturn$ = "unknown"
EndSelect
ProcedureReturn pReturn$
EndProcedure
ProcedureC eventTapFunction(proxy, type, event, refcon)
Protected keyCode = CGEventGetIntegerValueField(event, #kCGKeyboardEventKeycode)
Protected keyFlags = CGEventGetFlags(event) >> 16 & 255
Protected unicodeBuffer.q, bufferLen.i = 4
CGEventKeyboardGetUnicodeString(event, bufferLen, @bufferLen, @unicodeBuffer)
Protected text.s = PeekS(@unicodeBuffer, bufferLen, #PB_Unicode)
SetGadgetText(0, "keyCode : " + Str(keyCode) + #LF$ + "keyFlags : " + RSet(Bin(keyFlags), 8, "0") + #LF$ + "string : " + text)
EndProcedure
If OpenWindow(0, 0, 0, 220, 90, "keyCode tap", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
StickyWindow(0, 1)
StringGadget(0, 10, 10, 200, 70, "")
eventMask = #KeyDownMask | #FlagsChangedMask
eventTap = CGEventTapCreate(#kCGSessionEventTap, #kCGHeadInsertEventTap, 0, eventMask, @eventTapFunction(), #Null)
If eventTap
CocoaMessage(0, CocoaMessage(0, 0, "NSRunLoop currentRunLoop"), "addPort:", eventTap, "forMode:$", @"kCFRunLoopDefaultMode")
EndIf
Repeat : Delay(10): Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
working admin on OSX v10.13.6
Re: Global keyboard hook
Posted: Tue Aug 25, 2020 8:45 am
by deseven
I obviously tested what i wrote, not much more to add, sorry.
Try following the video maybe:
https://d7.wtf/s/Screen%20Recording%202 ... .37.06.mp4