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

Recommended Answers

Use PyUserInput
Install Pywin32 and

Jump to Post

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.

Jump to Post

Did you run the pip installer first ?

Jump to Post

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)

Jump to Post

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 …

Jump to Post

All 24 Replies

Re: Generating Keyboard Presses 80 80

Just in case you don't find an answer, try
https://www.google.com/search?hl=en&as_q=press+keyboard+button+using+python

The prior discussions cover the issues about why this is, and why it won't be too portable to other than the OS you are on.
Bob

Re: Generating Keyboard Presses 80 80

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
Re: Generating Keyboard Presses 80 80

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
Re: Generating Keyboard Presses 80 80

Did you run the pip installer first ?

Re: Generating Keyboard Presses 80 80

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.

Re: Generating Keyboard Presses 80 80

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)

Re: Generating Keyboard Presses 80 80

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?

Re: Generating Keyboard Presses 80 80

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.

Re: Generating Keyboard Presses 80 80

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.

Re: Generating Keyboard Presses 80 80

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.

Re: Generating Keyboard Presses 80 80

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.

Re: Generating Keyboard Presses 80 80

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.

Re: Generating Keyboard Presses 80 80

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?

Re: Generating Keyboard Presses 80 80

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

Re: Generating Keyboard Presses 80 80

973473947c19f1ee2d93c485e6c8d59e

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

Re: Generating Keyboard Presses 80 80

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

Re: Generating Keyboard Presses 80 80

3b8771b5bf7ccbecd99fc0fb966fc6bb

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

Re: Generating Keyboard Presses 80 80

You need to install setuptools. Follow these instructions.

Re: Generating Keyboard Presses 80 80

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

Re: Generating Keyboard Presses 80 80

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.

Re: Generating Keyboard Presses 80 80

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?

Re: Generating Keyboard Presses 80 80

Great! let us know if PyUserInput suits your needs.

Re: Generating Keyboard Presses 80 80

All is working fine with PyUserInput so I will be marking this as solved but just one more thing. Can this module use the hex codes shown here: http://msdn.microsoft.com/en-gb/library/windows/desktop/dd375731(v=vs.85).aspx

Re: Generating Keyboard Presses 80 80

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 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.