octalmage / robotjs

Node.js Desktop Automation.
http://robotjs.io
MIT License
12.37k stars 959 forks source link

Inconsistent behaviour when sending key combinations on macOS #272

Open mzmiric5 opened 7 years ago

mzmiric5 commented 7 years ago

Seems like sending a keystroke combination on macOS is not consistent in all applications. For example, sending Cmd+T with Chrome focused, opens a new tab, sending Cmd+B with Word focused, bolds the text, sending Cmd+/ with Atom focused, comments out the selected line. These all work as expected. But trying to send custom defined hotkeys to apps like OBS, GameShow or Discord results in any keystroke combo including any of the modifiers to not work. Sending a single key with no modifiers seems to only work if the specific app is focused. The custom hotkeys defined in those apps normally work regardless of the app being focused. On Windows, this works without any issues, so the problem seems to be OS specific to macOS.

Expected Behavior

Sending a keystroke combination should result in the same global behaviour that results from pressing that keystroke combination on keyboard both on Windows and macOS.

Current Behavior

Behaviour not consistent, and not captureable with Keyboard Viewer on macOS. Any custom global hotkeys don't seem to be responding to the keystroke combination being sent from node code if they include modifiers. Single key hotkey without modifiers seem to work if the targeted app is in focus. While this is the case on macOS, this problem does not exist on Windows.

Steps to Reproduce (for bugs)

  1. Define a custom hotkey using a modifier in any app
  2. Send a custom hotkey using keyTap or keyToggle from node
  3. Targeted app does not respond to the global hotkey
    
    const robot = require('robotjs');

robot.keyTap('2', ['alt']); // send an alt + 2 keystroke combination



## Context
I am developing an Electron application that allows users to map external midi hardware to custom keystrokes. Pressing a button on midi hardware fires a user defined keyboard events for single key or a keystroke combination (single key + any number of modifiers (shift, ctrl, alt, cmd).

## Your Environment
* RobotJS version: 0.4.5
* Node.js version: Node 7.6.0 for standalone tests, Electron 1.6.1 (Node 7.4.0) for use case
* npm version: 4.1.2
* Operating System: macOS Sierra 10.12.3
mzmiric5 commented 7 years ago

Further research indicates that on macOS, none of the keystrokes seem to be simulated globally, but only to the app that has focus.

I've found that in some apps the modifier values might appear different to those expected.

Some examples of what the further research resulted in: In GameShow:

In Discord:

In OBS:

I am assuming that the app having to be focused issue is caused by the Quartz Event simulating the keypress only sending the event to the application that is in foreground/focused.

Skhmt commented 7 years ago

Did you ever find a workaround for this issue?

mzmiric5 commented 7 years ago

Unfortunately no. We even tried switching the type of the keypress event that robotjs sends, with no luck

oktapodia commented 4 years ago

This issue has been closed because it has been inactive for > 180 days. You may reopen this issue if it has been closed in error.

Skhmt commented 4 years ago

Still a problem