Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In some games, special keys can't be triggered #228

Closed
FurryWolfX opened this issue Oct 14, 2023 · 15 comments · Fixed by #280
Closed

In some games, special keys can't be triggered #228

FurryWolfX opened this issue Oct 14, 2023 · 15 comments · Fixed by #280

Comments

@FurryWolfX
Copy link

FurryWolfX commented Oct 14, 2023

Describe the bug A clear and concise description of what the bug is.

In some games, special keys can't be triggered.

To Reproduce Steps or a minimal code example to reproduce the behavior.

for example:

enigo.key_click(Key::LShift); 

But letters work fine:

enigo.key_click(Key::Layout('r')); 

Expected behavior A clear and concise description of what you expected to happen.

The game I tested was Guild Wars 2.
LeftShift button bound to the skill, but it won't trigger.
I want to be able to trigger skills properly.

Environment (please complete the following information):

  • OS: Windows 10 - Rust 1.73 - enigo 0.1.3

Additional context Add any other context about the problem here.

@pentamassiv
Copy link
Collaborator

Thank you for the report. You wrote "in some games" does that mean that in general it works?
Also Windows makes a difference between Key::Shift, Key::LShift and Key::RShift. All these are different keys. The game might expect one of the other keys.

@FurryWolfX FurryWolfX changed the title In some games, special buttons can't be triggered In some games, special keys can't be triggered Oct 14, 2023
@FurryWolfX
Copy link
Author

Thank you for the report. You wrote "in some games" does that mean that in general it works? Also Windows makes a difference between Key::Shift, Key::LShift and Key::RShift. All these are different keys. The game might expect one of the other keys.

When I use the physical keyboard in the game, it all triggers the skills properly. But using enigo doesn't trigger it.
F1-F12 can't be triggered either.

@pentamassiv
Copy link
Collaborator

Can you please open Notepad and then run this slightly changed example

use enigo::{Enigo, Key, KeyboardControllable};
use std::thread;
use std::time::Duration;

fn main() {
    thread::sleep(Duration::from_secs(2));
    let mut enigo = Enigo::new();

    // select all
    enigo.key_down(Key::LShift);
    println!("Press key");
    thread::sleep(Duration::from_secs(5));
    enigo.key_up(Key::LShift);
}

Please try to press a letter key on your physical keyboard during the 5 seconds the example sleeps. If the letter is capitalized, we know that the Shift key works in general. Then we know that there must be some special case with the game

@FurryWolfX
Copy link
Author

Can you please open Notepad and then run this slightly changed example

use enigo::{Enigo, Key, KeyboardControllable};
use std::thread;
use std::time::Duration;

fn main() {
    thread::sleep(Duration::from_secs(2));
    let mut enigo = Enigo::new();

    // select all
    enigo.key_down(Key::LShift);
    println!("Press key");
    thread::sleep(Duration::from_secs(5));
    enigo.key_up(Key::LShift);
}

Please try to press a letter key on your physical keyboard during the 5 seconds the example sleeps. If the letter is capitalized, we know that the Shift key works in general. Then we know that there must be some special case with the game

It works fine in Notepad and other editors. But not in Guild Wars 2, I don't know why.

@pentamassiv
Copy link
Collaborator

Okay, thank you. That helps already to narrow it down. I probably need some time for this to investigate the issue.

@FurryWolfX
Copy link
Author

And in-game chatting also works, just not when binding skills.

@pentamassiv
Copy link
Collaborator

Maybe the old API to simulate input will work. Could you please try this changed example:

use enigo::{Enigo, Key, KeyboardControllable};
use std::thread;
use std::time::Duration;
use windows::Win32::UI::Input::KeyboardAndMouse::{KEYBD_EVENT_FLAGS, KEYEVENTF_KEYUP, VK_LSHIFT};

fn main() {
    thread::sleep(Duration::from_secs(2));
    let mut enigo = Enigo::new();

    // Simulate a key press
    unsafe {
        windows::Win32::UI::Input::KeyboardAndMouse::keybd_event(
            VK_LSHIFT.0 as u8,
            0,
            KEYBD_EVENT_FLAGS::default(),
            0,
        )
    };
    thread::sleep(Duration::from_millis(20));

    // Press other key (Change this if you'd like)
    enigo.key_click(Key::Layout('a'));

    // Simulate a key release
    thread::sleep(Duration::from_millis(20));
    unsafe {
        windows::Win32::UI::Input::KeyboardAndMouse::keybd_event(
            VK_LSHIFT.0 as u8,
            0,
            KEYEVENTF_KEYUP,
            0,
        )
    };
}

I don't know which other keys you want to press in order to see if Key::LShift works. Feel free to change this part of the example:

   // Press other key (Change this if you'd like)
   enigo.key_click(Key::Layout('a'));

@FurryWolfX
Copy link
Author

FurryWolfX commented Oct 15, 2023

Maybe the old API to simulate input will work. Could you please try this changed example:

use enigo::{Enigo, Key, KeyboardControllable};
use std::thread;
use std::time::Duration;
use windows::Win32::UI::Input::KeyboardAndMouse::{KEYBD_EVENT_FLAGS, KEYEVENTF_KEYUP, VK_LSHIFT};

fn main() {
    thread::sleep(Duration::from_secs(2));
    let mut enigo = Enigo::new();

    // Simulate a key press
    unsafe {
        windows::Win32::UI::Input::KeyboardAndMouse::keybd_event(
            VK_LSHIFT.0 as u8,
            0,
            KEYBD_EVENT_FLAGS::default(),
            0,
        )
    };
    thread::sleep(Duration::from_millis(20));

    // Press other key (Change this if you'd like)
    enigo.key_click(Key::Layout('a'));

    // Simulate a key release
    thread::sleep(Duration::from_millis(20));
    unsafe {
        windows::Win32::UI::Input::KeyboardAndMouse::keybd_event(
            VK_LSHIFT.0 as u8,
            0,
            KEYEVENTF_KEYUP,
            0,
        )
    };
}

I don't know which other keys you want to press in order to see if Key::LShift works. Feel free to change this part of the example:

   // Press other key (Change this if you'd like)
   enigo.key_click(Key::Layout('a'));

This example also does not trigger any skills that are tied to special keys (Shift and F1-F12 etc...)

I tried to simulate it using Python's Keyboard module and it works fine.

@pentamassiv
Copy link
Collaborator

Okay, thank you for trying it out. Back to the drawing board :/

@pentamassiv
Copy link
Collaborator

The same issue occurs with the game "Star Citizen"

@pentamassiv pentamassiv added the Windows windows specific label Feb 8, 2024
@pentamassiv
Copy link
Collaborator

Apparently it is best to send both, the scan code and the virtual key, in order for all programs to register the key input properly (e.g something like this https://github.com/Narsil/rdev/pull/113/files). Maybe that would solve this issue as well

pentamassiv added a commit to pentamassiv/enigo that referenced this issue Apr 18, 2024
Some programs seem to only read one of the two options. This should fix enigo-rs#228
pentamassiv added a commit to pentamassiv/enigo that referenced this issue Apr 19, 2024
Some programs seem to only read one of the two options. This should fix enigo-rs#228
@pentamassiv
Copy link
Collaborator

I created #280, which might address this issue. It would be great if you could test it to see if it fixes it.

@Aunmag
Copy link

Aunmag commented May 29, 2024

Same issue with Ctrl F1

@zardini123
Copy link

I also was having issues with controlling games on Windows 10 as well with enigo v0.2.1. Certain keys such as Space and Shift were not working, but others were working such as WASD.

I tried out your PR @pentamassiv in my Cargo.toml using enigo = { git = "https://github.com/enigo-rs/enigo.git", rev = "a204717" } and for my use case it solves all the problems. I haven't found any regressions in my case.

Thank you for the fix! I am looking forward to its release.

@pentamassiv
Copy link
Collaborator

That's great to hear, thank you for testing it. I will merge it within the next week and publish a new version :-)

pentamassiv added a commit to pentamassiv/enigo that referenced this issue Aug 14, 2024
Some programs seem to only read one of the two options. This should fix enigo-rs#228
pentamassiv added a commit to pentamassiv/enigo that referenced this issue Aug 14, 2024
Some programs seem to only read one of the two options. This should fix enigo-rs#228
pentamassiv added a commit to pentamassiv/enigo that referenced this issue Aug 14, 2024
Some programs seem to only read one of the two options. This should fix enigo-rs#228
pentamassiv added a commit to pentamassiv/enigo that referenced this issue Aug 14, 2024
Some programs seem to only read one of the two options. This should fix enigo-rs#228
pentamassiv added a commit to pentamassiv/enigo that referenced this issue Aug 14, 2024
Some programs seem to only read one of the two options. This should fix enigo-rs#228
pentamassiv added a commit to pentamassiv/enigo that referenced this issue Aug 14, 2024
Some programs seem to only read one of the two options. Fixes enigo-rs#228
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants