Debugging key event flow in OS X before it reaches an app - macos

I have a user who reports that a particular keystroke isn't working (control-apostrophe) in my app on OS X. This does work for him on other machines.
I added some logging and my app never receives the NSEvent for the keyDown. Disabling universal access for assistive devices didn't fix it either. Nothing in keyboard shortcuts in system preferences uses that shortcut. Is there any way to trace a keystroke's path through the OS to find out why my app doesn't get it? Is this something dtrace can do?

This is an old question, but for anyone needing to do this I'll give an answer. I'm not going to go through the usual ways of tracking down a kb shortcut that is going bad, but you will have to make sure you do not have it set in system services, keyboard maestro, quicksilver and all of those apps that allow kb customization. Check this first. If you don't have a lot of kb shortcuts set through sys preferences, you might want to consider just deleting your plist file and letting the system default back to a fresh state. search for com.apple.symbolichotkeys.plist and delete this. If this doesn't solve your issue or you don't want to delete the plist then run dtrace's iosnoop command from terminal. This will just dump everything going on. Hit the key and ⌃ c to stop the output and inspect the output. You could output this to a text file and do some grep magic on it, but this should be enough to head in the right direction.

Related

Unexpected -psn_0_65552 command line argument after mac reboot

When I reboot my mac, if an application that I'm developing was running during reboot, macos starts that application right after restart with "-psn_0_65552" argument. I assume that this is related to "Reopen windows when logging back in" checkbox enabled on restart prompt. I tried to find any documentation about this argument, but failed. So, my questions:
Is this behavior documented somewhere?
Any other weird command-line arguments my application should be able to handle?
Can I avoid my application reopening after mac restart?
Nobody else has offered you anything yet so maybe this will help:
Is this behavior documented somewhere?
The state preservation and restoration mechanism is at least partially documented through the methods you can call to participate in it, e.g. NSWindow's restorable & encodeRestorableStateWithCoder:, NSWindowDelegate's window:willEncodeRestorableState:, NSApplication's restoreWindowWithIdentifier:state:completionHandler: etc. However how the various AppKit classes preserve and restore all the information is not publicly documented (there is a statement to that effect in the docs somewhere).
Any other weird command-line arguments my application should be able to handle?
Anybody's guess.
Can I avoid my application reopening after mac restart?
Maybe. Some apps don't restart on relaunch, whether their authors control this or its happenstance I've no idea.
Here's a suggestion: write yourself a very basic GUI app which just saves the arguments and environment (you can use NSProcessInfo to get them all) at launch time to user defaults. Lunch your app from Xcode, Finder and Terminal. Reboot while its open. Look at the user defaults (look in its sandbox and open the .plist in Xcode). Notice anything? Want to rely on it? Your call.
Can you think of others way to track your app launches and user-initiated quits? Maybe setting and removing a flag in user defaults? Have fun!
HTH

Recording X windows events

I am a novice when it comes to X windows but have some knowledge of Unix as such.
My project requires me to track user input and output on X window system. For instance, if the GUI is used to configure a route, I would like to know what application is used and what route has been configured. So far, I have explored the following options with partial success.
1)Tried to hook functions like XDrawString and XDrawText using LD_PRELOAD.
2)Used xwininfo to obtain window id and tools like xev.
3)Looked through similar discussions in this forum especially on xev and xinput
1)May not work with if X11 is statically linked? Not sure.
2)xev does not record key press events for a file edited with gedit or attempting to rename a file from the GUI
3)I am trying to go through X windows system internals.
I am pretty discouraged so far. Any input/pointer will be appreciated.
I think you want the cnee program from the Xnee project, which uses the X window system Record extension. The examples that I see for cnee are almost always about recording input events, but, according to the Xnee manual at https://xnee.files.wordpress.com/2012/10/xnee1.pdf, section 3.2.1 ("Record"), "Xnee can record the whole X11 protocol, not just mouse and keyboard events."
Regarding font operations, I believe that X font facilities, mostly through the X font server, evolved over time too, so it might be the case that the applications that you care about are doing X font operations which you can trace.

Control to record user hotkeys in a OSX application

I'm dealing with Hotkey registration, i can't found a way to make user able to register its hotkey.
I tried ShortcutRecorder but it seems impossible to make it works, for me it crash with some error messages related to CG.
I'd like to know if there is a way to make ShortcutRecorder works with a ARC environment and Xcode 4.0 or if you use other control to grab user Hotkeys.
You should check out this fork of ShortCut Recorder:
https://github.com/youknowone/shortcutrecorder
The original appears to have not been updated in some time but I was able to get this fork running on 10.7 without any changes.

How do I use stackshot to debug my application?

I've recently been told about a useful debugging tool on Mac OS X called stackshot, however I don't know how to invoke it. My understanding from the documentation is that it runs as a daemon, but the man page also reads as if it can be invoked from the command line. How would I go about leveraging stackshot for my currently-running application?
sudo /usr/libexec/stackshot -i
Stackshot is intended for debugging system-wide issues (deadlocks etc), not single applications; as such it creates a stack dump of the entire system, not a single application.
With the default configuration, this will create a stack shot in /Library/Logs/stackshot.log
Apple has asked me to produce a Stackshot on my iOS device. To do so, when the issue occurs, simultaneously hold down and release the Home and a volume button.
Then, synch your device with iTunes.
After synching, your stackshot log file will be stored on the computer. I don't know where it is in Windows, but on a Mac it is in:
~/Library/Logs/CrashReporter/MobileDevice//stacks-yyyy-mm-dd-xxxxxx.log
stackshot no longer appears to be included with OS X, at least not in 10.11. However you can still take a sample of your app using the /usr/bin/sample tool.
This is also accessible using Activity monitor:
Open Activity Monitor
Double click your app
Click the Sample button in the bottom left corner
That will allow you to see what is currently happening in your app so that you can debug CPU usage issues without running the app from Xcode.

Better terminal in Mac OS X -- reversing the control and command key-mappings

I'm trying to have the same KDE Konsole experience within Mac OS X.
Here's my (overly complicated?) setup:
I have Control and Command swapped at the System Preferences level. (Can't live without this)
Parallels lets you, at the Parallels application level, also reverse Control and Command. So I can undo the System Preferences setting (and get the setup I want within virtual Linux)
I want this same per-application-opt-out for the Mac OS X Terminal app. Is it possible?
The solution you're looking for is KeyRemap4MacBook. There is a Tiger, Leopard, Snow Leopard, and Lion version.
Once installed, goto System Preferences -> KeyRemap4MacBook
Then select the following options:
Change Command_L Key (Left Command)
---> Command_L to Control_L (except Terminal, Virtual Machine, RDC)
Change Control_L Key (Left Control)
---> Control_L to Command_L (except Terminal, Virtual Machine, RDC)
You can repeat this for Command_R (Right Command) and Control_R (Right Control) also if you desire. Tested and working on my Macbook.
You can simply ssh into the Linux/Unix system and run X11 programs direct to your Mac screen: ssh -X user#host_or_ipaddress, login, and just run the X11 programs you want (e.g. emacs&) and the X11 apps will appear on the Mac display.
Pros:
X11 windows work just like any other window, including Exposé goodness, etc...
No need to work only inside the Parallels console window
Same solution works with any Linux/Unix system, remote or virtual
ssh connection is secure even over the internet
Tech info:
"ssh -X" turns on X11 forwarding for the ssh connection, i.e. the X11 display connection is tunneled through ssh securely
"ssh -X" also handles X11 authentication tunneling
X11.app is automagically started on OSX by launchd when needed
X11 can connect to displays over the network, which is one of the few cool things about it ;-)
There is very good and key-mapping flexible terminal: iTerm2
My favorite set: iTerm + zsh + oh-my-zsh
The 2016 solution is to use Karabiner open-source program which allows you to remap modifier and other keys with very fine granularity, for example
Remap only the left ⌘ Cmd or Option key.
Remap a key only for specific applications, e.g. only inside Terminal, Emacs, or virtual machine.
For example, here's how to remap left ⌘ Cmd key to act as Ctrl only inside Terminal (and leave the right one unaffected so that you could still use e.g. ⌘ Cmd + Tab to switch between apps):
You can customize the command keys used for an individual application in System Preferences > Keyboard & Mouse > Keyboard Shortcuts. I think (if I understand correctly what you're trying to do) that this might allow you to accomplish your goal. You could remap all of Terminal's command keys to use control instead of command, to get them out of your way... but then you might need to do a lot of customization on the machines you ssh into, so that they use Command instead of control
It seems that you're going to have to do an ENORMOUS amount of work just to allow you to use your pinky instead of your thumb for the modifier key.
Another possibility: user preferences can be manipulated by the "defaults" command. I haven't been able to find a way to use this to control they modifier key mappings, but it should logically be possible (if you're willing to do a lot of digging). If so, then you could write short scripts to switch back and forth between Mac default and your swapped mode. Trigger the scripts with Quicksilver, and whenever you use Terminal you can call one script, and whenever you leave it you can call another. Again, a big pain to achieve what you want, but it might be possible.
I don't think there's a clean and simple solution.
I've seen third-party programs that give more control over manipulating modifier keys, if you're willing to install them (probably kernel extensions). They might be able to do what you want, but I don't recall the names. If you google for programs to fix emacs and vi keys you might find them.
Good luck.
I had exactly the same problem as you. I've remapped Command to CAPS Lock, and Control to Command, but as a frequent Linux user I want both setups to be as similar as possible. This is how I solved it:
Install Keyboard Maestro (not free, but totally worth it), and set it up to run at login.
Inside KM, define macros to send CMD+{key} to CTRL+{key} inside Terminal.
If you want to remap a lot of keys this is a lot of work. But I've already done it myself, you can just download this file kmmacros. Double-clicking is enough to install it. A couple of caveats:
You need to set Terminal to use option as a meta key (Terminal > Preferences > Keyboard).
The bindings are only for emacs-mode. This cheat sheet should be helpful.
Most default shortcuts don't work anymore (CMD-N, CMD-C, ...), and the menu in Terminal does not reflect this. For some conflicts (New Window, Close Window), I've taken the ones from Gnome.

Resources