0

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

4
Contributors
24
Replies
180
Views
2 Years
Discussion Span
Last Post by Gribouillis
Featured Replies
  • Use [PyUserInput](https://github.com/SavinaRoja/PyUserInput) Install [Pywin32](http://www.lfd.uci.edu/~gohlke/pythonlibs/#pywin32) and [Pyhook](http://www.lfd.uci.edu/~gohlke/pythonlibs/#pyhook) Install [pip](http://www.lfd.uci.edu/~gohlke/pythonlibs/#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') Read More

  • 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. Read More

  • Did you run the pip installer first ? Read More

  • 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) Read More

  • 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 … Read More

2

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 by snippsat

Votes + 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
1

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.

Votes + 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?
0

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.

1

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)

0

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
1

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 by Gribouillis

0

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.

1

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.

0

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.

1

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 by Gribouillis

0

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?

0

973473947c19f1ee2d93c485e6c8d59e

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

Attachments pythonpath_commandprompt.png 9.06 KB
0

3b8771b5bf7ccbecd99fc0fb966fc6bb

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

1

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

Edited by snippsat

0

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.

0

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?

0

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 by Gribouillis

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.