Recently, I've been trying to find out how to make my computer think that a button on the keyboard has been pressed using python. I want the whole system to think the key has been pressed. I need this to be possible in windows 7 and 8, on the latest version of python. Any code and explanations about how to use any modules that might be helpful would be very helpful.

Thanks,

Patrick

Use PyUserInput
Install Pywin32 and Pyhook
Install pip
Then do pip install PyUserInput

from pykeyboard import PyKeyboard

k = PyKeyboard()    
# pressing a key
k.press_key('H')
# which you then follow with a release of the key
k.release_key('H')

Edited 2 Years Ago by snippsat

Comments
This helpful so far but I have no idea how to use pip which probably sounds pretty stupid so please can you just tell me how to use pip
good idea

To use pip, in Windows, execute the program cmd, and type

pip install PyUserInput

in cmd's terminal window.

In ubuntu linux, open a terminal and type

sudo pip install PyUserInput

give your login password on request.

Comments
I opened command prompt and typed in "pip install PyUserInput" and it returns: "'pip' is not recongnized as an internal or external command, operable program or batch file." Whats going on I downloaded it before and it doesn't seem to be working?

When I downloaded it I clicked run and didn't pick save or open at all and it opened a setup wizard that said it is was powered by python. Was there anything else I was supposed to do? Could it be to do with the fact I'm in Windows 8.

Perhaps the pip executable is not on your path. Find the location of pip.exe in your file system. (If you can't find it, install Locate32 to find it)

I typed in 'pip.exe' in search and here attached is a screenshot of the result. Am I looking for one of these files and if so what do I do with it?

Attachments pip.exe_search_result_.png 187.88 KB

I see that you have several installs of python on your computer. First type

python -c "import sys; print(sys.executable)"

in a cmd prompt, to see which is the default python. Assuming it is python 3.4, make sure the environment variable PATH contains the folders C:\Python34 and C:\Python34\Scripts (and not the other pythons). Then the pip command should be available. You should also check that the PYTHONPATH variable exists and contains your python library folders(perhaps C:\Python34\Lib).

See this page on python in windows.

Edited 2 Years Ago by Gribouillis

I'm very bad at using windows so that really doesn't make much sense. I'm just doing this for a project with an arduino that is fast becoming very complex. I tried the first command and the command prompt told me that python wasn't reconginsed.

You must set the PATH environment variable. It must contain a python directory (C:\Python34). Also, if you dont need 2 versions of python 3, you could uninstall python 3.3.

I tried 'set PYTHONPATH=%PYTHONPATH%;C:\Python33' and that didn't help. By the way I'm aiming to use python3.3.5 because of stuff to do with other modules I'll be using.

PATH and PYTHONPATH are 2 different things. You must update the PATH to contain C:\Python33, but be careful not to remove useful folders.

Edit: this tutorial may help you.

Edited 2 Years Ago by Gribouillis

Python is now recognised which is great but I'm still not getting anywhere with pip. Do I have to do something similar or reinstall it?

3b8771b5bf7ccbecd99fc0fb966fc6bb

Now I'm getting this error message. I this because something is missing from pip?

Setuptool can be found here.
Correct environment variable is ;C:\python33\;C:\python33\scripts\;
Look that's is correct by looking at Path.

Edited 2 Years Ago by snippsat

I finally got pip working and now I've installed PyUserInput I just have to is see if that works. Thanks for all the help with getting it all to work.

After all that what I needed to do is send the key presses to a game while the program ran in the background. Can this still be done?

Can this module use the hex codes

It probably does as this source code suggests.

You could write some tests like

>>> from pykeyboard import PyKeyboard
>>> k = PyKeyboard()
>>> print(hex(k.end_key))

and see if it uses the correct hex codes. In your code however, it is better to use k.end_key instead of VK_END or 0x23, because k.end_key is cross-platform (is it really ?). For example in my linux OS, it is 0x73.

Edited 2 Years Ago by Gribouillis

This question has already been answered. Start a new discussion instead.