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')
commented: good idea +14
commented: 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 +0

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.

commented: 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? +0

Did you run the pip installer first ?

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?

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.

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.

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?

For pip, you need C:\Python33\Scripts in the PYTHONPATH

973473947c19f1ee2d93c485e6c8d59e

Here's what happens now that I've added thse things into PYTHONPATH.

Sorry, the folder must be added to PATH (provided there is a file C:\Python33\Scripts\pip.exe)

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.

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?

Great! let us know if PyUserInput suits your needs.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.