Question

Mac app cheatsheet ( http://www.cheatsheetapp.com/CheatSheet/ ) or keycue ( http://www.ergonis.com/products/keycue/ ) have the function that gather all the keyboard shortcuts of an active application. I want to know how can I realize this function.

Was it helpful?

Solution

On the Mac, there are a few different ways to do this, including:

  • Enumerate the menus and menu items and read the keystrokes out of them. This again can be done in three different ways:
    • UI scripting. You do this by talking to the app "System Events" (see its dictionary in AppleScript Editor, specifically the Processes Suite) via AppleScript, ScriptingBridge, appscript, F-Script, etc. The disadvantage to this is that the user must have enabled assistive access for this to work.
    • Insert your own code into every app and use the NSMenu APIs.
    • Inserting your code into every app can be done in a variety of different ways, but it's probably best to just write a SIMBL plugin so you don't have to figure out how to do it yourself.
    • From inside any app, if it's got an ObjC runtime, you can access all of the Cocoa classes that run the menubar. In some cases, this can even get you dynamically-generated menu items that don't currently exist but could.
    • The downside is that it doesn't work on Carbon apps, but there are fewer of those left every day.
    • If you're interested in this one, inject fscript into an app and start playing with the object browser and scripting interface, and you should be able to figure it out from there. Then you can translate what you've learned into the language of your choice.
    • Insert your own code into every app and use the Carbon Menu Manager APIs in HIToolbox.
    • These are found in Carbon/Menus.h; they're more fiddly, and have much less of a future, but at present they'll work for both Carbon and Cocoa apps.
  • Look at the NIB files embedded in the app. This is pretty simple, but only handles menus created from the NIB.
  • Look at the shortcuts the app registers with the OS. This is also pretty simple, but it's next to useless in modern OS X, because only global shortcuts usually get registered. (If you open the Keyboard pane of System Preferences, you can see what's there.)

On Windows, I believe this involves sending WM_* messages of some kind, but you probably want to make that a separate question (that's not tagged osx) instead of attaching it to this one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top