I have no idea if this is a smart way to do it, but it was the only way I could get it to work. Watcher = hs.(applicationWatcherCallback) Create and start the application event watcher Emacs just lost focus, enable our hotkeys Emacs just got focus, disable our hotkeysĮlseif (eventType = hs.) then Define a callback function to be called when application events happenįunction applicationWatcherCallback(appName, eventType, appObject) since it will never be triggered from the keyboard Create a modal hotkey object with an absurd triggering hotkey, Hotkey bind to set window to a specific size is easy & convenient to make with Hammerspoon and works nicely if you only need few sizes and use similar resolution screens, but is not ideal when constantly switching between desktop and laptop screens with different sizes & resolutions. Return function() hs.eventtap.keyStroke(modifiers, key, 0) end I'll try and post an example later but I'm not at my computer at the moment. Search the issues and you should find some examples of watchers that should point you in the right direction. we've attempted rewriting hs.hotkey to use the eventtap infrastructure in the past and it just isn't fast enough to be enabled all of the time for key and flag events - you have to catch them all then determine if its one you care about. Watching for a hotkey with hs.eventtap seems like it should be more flexible, but if your Mac gets busy, you may notice dropped keystrokes (and not just the ones you're trying to match on). ![]() search the issues - this has come up before. any attempt to create a synthetic version of the hot key combination with hs.eventtap.event will in turn be caught by hs.hotkey, thus creating an infinite loop. Hotkey's as defined with hs.hotkey, when enabled, are always caught and can not be "passed through". However, this script should give you a good starting point.If you're wanting the key combo to do something for most applications, but not for a few specific ones, you're better off using a window filter or application watcher and enabling/disabling the hotkey(s) when the active application changes. Also, depending on the specifics of your use case, there may be other edge cases to consider, such as what happens if the target application is not already running. Note that you will need to replace "Target Application Name" with the actual name of the application you want to send keystrokes to. Finally, the saved focus is restored after the keystrokes are sent. ![]() Detect when CMD+N is pressed and create key events to simulate CMD+F. Detect when CMD+F is pressed and return true to block it. Setup an event tap for the key down event. The hs.application.get() function is used to get a reference to the desired target application, and the activate() method is used to bring it to the foreground before sending keystrokes. I made an example which blocks CMD+F (Find) in Google Chrome and remaps CMD+N (New Window) to this functionality instead. ![]() In this script, the saveFocus() and restoreFocus() functions are used to save and restore the previously focused application. Local targetApp = hs.application.get("Target Application Name") Here’s an example script that does this: - define your keystroke function as before One possible solution is to use Hammerspoon’s hs.application module to focus on the specific application you want to send keystrokes to before executing your script, and then refocusing on your previous application afterward. As an AI language model, I am not familiar with keystroke or Hammerspoon specifically, but based on my understanding of your question, it sounds like you want to send keystrokes to a specific application without interrupting your normal keyboard usage in another application.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |