BlueM / cliclick

macOS CLI tool for emulating mouse and keyboard events
https://www.bluem.net
Other
1.59k stars 116 forks source link

BUG Catalina moving mouse releases the KD #101

Closed cotfas closed 3 years ago

cotfas commented 4 years ago

On Catalina 10.15.4 using the Version 4.0.1, released 2018-04-10

Executing from terminal: cliclick kd:shift,ctrl,alt,cmd t:t

It sends the hotkey, but if I execute the command and move the mouse while I execute the command from what I see the kd keys are released and then the t keyword is shown in the terminal

Working:

cotfas:build work$ cliclick -m verbose kd:shift,ctrl,alt,cmd t:t Hold shift key down Hold ctrl key down Hold alt key down Hold cmd key down Type: “t” cotfas:build work$

Not working when moving the mouse:

cotfas:build work$ cliclick -m verbose kd:shift,ctrl,alt,cmd t:t Hold shift key down Hold ctrl key down Hold alt key down Hold cmd key down Type: “t” t cotfas:build work$ t

*Notice the "t" written in the terminal and the hotkey is not sent, How can I bypass this issue? **At least to lock the mouse until the command finishes the execution

Thank you

Edit:

Simple test in terminal:

cotfas:build work$ ./cliclick -m verbose kd:cmd kd:t Hold cmd key down Hold t key down cotfas:build work$

Opens a new tab, supposed scenario

Executing the command, but this time moving the mouse as well:

cotfas:build work$ ./cliclick -m verbose kd:cmd kd:t Hold cmd key down Hold t key down cotfas:build work$ t

Does not open the new tab in terminal, it releases the t key

cotfas commented 4 years ago

I found it working with the following code:

struct timespec waitingtime; waitingtime.tv_sec = 0; waitingtime.tv_nsec = 1 * 1000000; // Milliseconds

CGEventRef f4 = CGEventCreateKeyboardEvent(NULL, 17, true); CGEventFlags flags = kCGEventFlagMaskShift | kCGEventFlagMaskControl | kCGEventFlagMaskAlternate | kCGEventFlagMaskCommand; CGEventSetFlags(f4, flags); //CGEventTapLocation location = kCGHIDEventTap; CGEventTapLocation location = kCGSessionEventTap; CGEventPost(location, f4); CFRelease(f4);

nanosleep(&waitingtime, NULL);

//releasing CGEventRef f4r = CGEventCreateKeyboardEvent(NULL, 17, false); CGEventPost(location, f4r); CFRelease(f4r);

BlueM commented 3 years ago

Cliclick is just a tool for emulating key/mouse events. It does not claim to intercept other key/mouse events, nor is it able to control the internals of the macOS event system. In other words: cliclick does not send any ku command, but the reason for this behaviour is in macOS itself. Therefore, this is not a bug.

Your code is working as you send an atomic event – the modifier keys are sent together with the key event. On the other hand, cliclick emits a sequence of events without understanding the semantics (for example, it emulates pressing ⌘, then T, then releasing ⌘, but does not recognize this should be ⌘T) which – from macOS’ perspective – are distinct events. And as such, other events observed by macOS (such as a mouse movement) might change how the events are processed.

Theoretically, this could be changed by trying to understand behind the logic/motivation behind the commands, but apart from the fact, that this would be a complete rewrite, I am pretty sure this would break some use cases where cliclick works right now.