It is currently Fri Oct 30, 2020 2:57 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 36 posts ]  Go to page Previous  1, 2, 3
Author Message
 Post subject: Re: Global keyboard hook
PostPosted: Mon Aug 24, 2020 12:36 pm 
Offline
User
User

Joined: Sat Aug 15, 2020 6:06 pm
Posts: 14
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Global keyboard hook
PostPosted: Mon Aug 24, 2020 2:27 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Jan 12, 2011 3:48 pm
Posts: 217
Location: Serbia
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).

_________________
PB examples collection
pb-osx-globalhotkeys
pb-osx-notifications


Top
 Profile  
Reply with quote  
 Post subject: Re: Global keyboard hook
PostPosted: Mon Aug 24, 2020 6:38 pm 
Offline
User
User

Joined: Sat Aug 15, 2020 6:06 pm
Posts: 14
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Global keyboard hook
PostPosted: Tue Aug 25, 2020 3:52 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Jan 12, 2011 3:48 pm
Posts: 217
Location: Serbia
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:
<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.

_________________
PB examples collection
pb-osx-globalhotkeys
pb-osx-notifications


Top
 Profile  
Reply with quote  
 Post subject: Re: Global keyboard hook
PostPosted: Tue Aug 25, 2020 5:13 am 
Offline
User
User

Joined: Sat Aug 15, 2020 6:06 pm
Posts: 14
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:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Global keyboard hook
PostPosted: Tue Aug 25, 2020 8:45 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Jan 12, 2011 3:48 pm
Posts: 217
Location: Serbia
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

_________________
PB examples collection
pb-osx-globalhotkeys
pb-osx-notifications


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 36 posts ]  Go to page Previous  1, 2, 3

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 13 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye