MediatedCommunications / WindowsInput

Capture and Simulate Keyboard and Mouse Input
MIT License
115 stars 17 forks source link

WindowsInput can't send keys to games #7

Closed cherry3048 closed 3 years ago

cherry3048 commented 4 years ago

I've tried and still doesn't work in the game ( Blade and Soul ), can someone help me, please.

my code

WindowsInput.Simulate.Events() .Hold(KeyCode.T).Wait(1) .Release(KeyCode.T) .Invoke();

TonyValenti commented 4 years ago

Make sure to await that call!

await WindowsInput.Simulate.Events() .Hold(KeyCode.T).Wait(1) .Release(KeyCode.T) .Invoke();
cherry3048 commented 4 years ago

Still doesn't work in the game, I don't know what's wrong.

    public async Task tKey() {            
        await WindowsInput.Simulate.Events().Hold(KeyCode.T).Wait(1).Release(KeyCode.T).Invoke();
    }  
    private void timer1_Tick(object sender, EventArgs e)
    {
        while (true)
        {
            if(MouseButtons == MouseButtons.XButton2)
            {
                tKey();
                Thread.Sleep(10);
            }
            break;
        }
    }`
TonyValenti commented 4 years ago

try this:

public async Task tKey() {            
        await WindowsInput.Simulate.Events().Hold(KeyCode.T).Wait(1).Release(KeyCode.T).Invoke();
    }  
    private async void timer1_Tick(object sender, EventArgs e)
    {
        while (true)
        {
            if(MouseButtons == MouseButtons.XButton2)
            {
                await tKey();
                Thread.Sleep(10);
            }
            break;
        }
    }`

Also, thread.Sleep is bad. You should use "await task.delay(...)"

Also, you'd be better to launch a background task than use a timer. ie.

var ToDo = Task.Run(()=>StartSendingKeys());

shukenmg commented 4 years ago

I have the same issue sending inputs to software like Cemu or YUZU.

But software like chrome works.

I use this, WindowsInput.Simulate.Events().Click(key).Invoke();

Any ideas?

Thanks.

cherry3048 commented 4 years ago

I've used await tKey(); and then I got the unhandled exception error, I did set execution level to <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> or <requestedExecutionLevel level="highestAvailable" uiAccess="false" />, but still doesn't work.

Inner exception InvokeDispatcherException: A simulated input command was not sent successfully. This generally occurs because of Windows security features including User Interface Privacy Isolation (UIPI). Your application can only send commands to applications of the same or lower elevation. Similarly certain commands are restricted to Accessibility/UIAutomation applications. Please refer to the project home page and the code samples for more information.

Exception .txt

TonyValenti commented 4 years ago

If I had to guess, those apps might not be listening for input through windows. They might be listening through DirectInput.

If you right-click on your app and select "Run as Administrator", does it work?

cherry3048 commented 4 years ago

Run as Administrator work, if I run without administrator nothing will happen, I can confirm SendInput and WindowsInput can be work, I don't know what's wrong. I have tried both methods, sometimes it works fine and sometimes it fails, most times it fails.

cherry3048 commented 4 years ago

I've tried about 20 to 25 times with this code, it's worked about 4 to 5 times in my game. await WindowsInput.Simulate.Events().Hold(KeyCode.T).Wait(1).Release(KeyCode.T).Invoke();

The way I try to run code is: Run code > Fail > Fail > Fail > Work > Fail > ..........> Work.

TonyValenti commented 3 years ago

The reason that it works when you run as Administrator is because your game is likely running with elevated priviledges which causes windows to separate out the messages. This prevents a low-security app from sending input messages to a high-security app.