lucassklp / Desktop.Robot

A library used to control your mouse and keyboard programmatically in .NET Core
MIT License
141 stars 24 forks source link

OSX ARM - KeyPress(Key.Right) jumps all the way to end of line #59

Open cdhanna opened 3 months ago

cdhanna commented 3 months ago

I have this code,

await Task.Delay(500);
var robot = new Robot();
robot.AutoDelay = 1000;
robot.KeyPress(Key.Right);
Console.Beep();

And if I run the code while my cursor is sitting in the middle of some text, then the cursor will jump all the way to the end of the line.

I would expect that the cursor would only move 1 space to the right. However, the actual behaviour is closer to me holding COMMAND and then pressing the right-arrow key. (that will jump to the end of text in osx).

I've tried manually doing a KeyUp(Key.Command) before the KeyPress(Key.Right), but that doesn't seem to make a difference.

I'd love to try and help solve this, but the call to the .so file seems opaque to me. How are those files generated?

cdhanna commented 3 months ago

Oh I see, there is a file, /Desktop.Robot/Source/osx_arm/main.mm and I'm assuming I can build this similar to the osx build.sh script

cdhanna commented 3 months ago

Oooh, okay I fixed it. I'll try and open a second PR in a bit. (I have a pending one that I need my local version for, and I don't want to do the gits right now)

But, in general, I added this function to the Source/osx_arm/main.mm file,

    void sendCommandDownWithoutHotkey(short input){
        CGKeyCode inputKeyCode = input;
        CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
        CGEventRef saveCommandDown = CGEventCreateKeyboardEvent(source, inputKeyCode, true);
        CGEventPost(kCGAnnotatedSessionEventTap, saveCommandDown);
        CFRelease(saveCommandDown);
        CFRelease(source);
    }

And then also this one,

    void sendCommandWithoutHotkey(short input){
        sendCommandDownWithoutHotkey(input);
        sendCommandUp(input);
    }

And then I changed the KeyPress implementation in the ARM Robot implementation to use that new sendCommandWithoutHotkey method. And now it works as expected.

Oh, and I re-ran make in the Source/osx_arm/ directory.