boppreh / keyboard

Hook and simulate global keyboard events on Windows and Linux.
MIT License
3.77k stars 433 forks source link

is it possible to get the output in the active window? #524

Open jorigel opened 2 years ago

jorigel commented 2 years ago

Hi, my script is working so far . . . But I expected the output happens in the active window. In my case it happens in the terminal-window where I started the script. What am I doing wrong? Or is that not possible in such a way calling it? Is it the "print"-order doing this and what can I do about it make it showing up in the active window?

thanx for Ur attention jori

jorigel commented 2 years ago

Hi folks, I'm trying to explain my problem with a little piece of code:

begin import keyboard

keyboard.wait('ctrl+alt+p') keyboard.write('Test')

print('Test')

end

I'm expecting a written 'Test' in the active window as soon as the sequence ctrl+alt+p is pressed. The script starts working but ends without writing the text in the active window (it shows up in the window where the script was started if using the print-command instead of keyboard.write but not in the active window).

Anybody able to explain, what I'm doing wrong to solve the problem? jori

freerror commented 2 years ago

You would probably do that something like this:

import keyboard

def test_func():
    print("Test")
    keyboard.write("Test")

keyboard.add_hotkey("Ctrl+Alt+p", test_func)
keyboard.wait()

But I would expect your program to work assuming you uncomment #print as it should stop waiting and continue after the keyboard.wait call. Tested and it works on my machine (displayed "Test" in both the active text editor and in the console).