PyAutoGUI seems to ignore numpad key presses - python

I'm using Windows, Python, and PyAutoGUI to try to automate some activities in Minecraft as a fun project.
I have been successful with using PyAutoGUI to switch to Minecraft once I start the script in Visual Studio Code, click on the "Back to Game" button, and then move the avatar forward by holding the "w" key.
I am using a 3rd party program called "NeatMouse" to use my numpad keys in place of using a mouse. The numpad 8 button is equivalent to moving the mouse up, which in Minecraft causes your avatar to look up. When I press this button myself in Minecraft, it works as expected, so it must be the case that NeatMouse is not the problem.
When I try to have PyAutoGUI replicate this same key press, it seems like nothing is happening.
I have tried different combinations of
pag.press()
pag.hold()
pag.keyDown() & pag.keyUp()
These functions do work for the WASD keys, so I know that Minecraft is able to receive keyboard input from PyAutoGUI, so that generally must not be the problem.
Here is a sample code block of what I have tried.
import pyautogui as pag
def align_vertical_facing_axis(target):
facing = get_facing_axes()[1]
# Looking down
if facing > target:
print('torture')
pag.keyDown('num8')
sleep(1)
pag.keyUp('num8')
get_facing_axes() is a function I wrote to retrieve the axes the avatar is facing as a tuple from the Minecraft debug screen. A positive value in the [1] index means the character is looking at a downward angle. When I run this script, it does print "torture" to my console so I know for sure it is entering that "if" block.
That was the long version of the explanation, the short version is: PyAutoGUI won't press the numpad keys, what do??

After some research, pyautogui.platformModule contains the mappings for numpad.
Here are the windows key mappings: https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
import pyautogui as pag
#This is where you update key mappings, num8 is now referring to the virtual key code VK_NUMPAD8 or 0x68
pag.platformModule.keyboardMapping.update({'num8':0x68})
#To call it, you use the mapping name you gave to the virtual key code
pag.press('num8')
Use the list I gave you for other key mapping values such as 0x68 for num8 and so on.

Related

How to Make Keypresses Using PyAutoGUI

I am using PyAutoGUI to try to code a macro. For this macro, I need to press a key to select something. To interact with the screen, I am using PyAutoGUI. Any ideas on how to press the key?
I have tried using this command.
pyautogui.press("s")
I'm not sure if anyone has helped out here or if you got this resolved in the end.
First, the documentation can be found here: https://pyautogui.readthedocs.io/en/latest/quickstart.html#keyboard-functions#
When passing a keystroke, you want to use "typewrite" and not "press"
Press "S"
pyautogui.typewrite("s")
Press Hotkey (Ctrl + S)
pyautogui.hotkey("ctrl" + "s")
Press key as a variable
pyautogui.keydown(variable)

pyautogui keydown and keyup arent working properly python

so I'm using pyautogui to type, and I'm trying to hold a key down for more than a second but I've ran into this problem where the key only types a single letter
import pyautogui
import time
pyautogui.keyDown("w")
time.sleep(2)
pyautogui.keyUp("w")
my output is "w"
but my output should be "wwwwwwwwwwwwwww" since I'm holding down the key?
the same thing occurs when im using the press function for pyautogui,
pyautogui.press("w") #but instead of pressing a single key, it totally just doesnt get outputted but only works for main keyboard functions like windowsKey and enter
if this is wrong, is their a way i can make it so I'm holding down the key?
From the Documentation it seems that it's not possible to do the way you have tried, however this function can help for 'holding down' a letter for a set number of seconds:
def hold_character(hold_time, character, interval=0.1):
pyautogui.write(character * int(hold_time / interval), interval=interval)
hold_character(2, 'w')
...gives the 'wwwwwwwwwwwwwww' effect for me

Getting Key Press Information in Python

I am trying to write a Python program that reads input from the command line like the input builtin function. I would also like to be able to detect key presses of certain keys (such as the home, end, page up, page down, function keys, etc.) When one of those keys is pressed I would like to fire off an event (i.e. call a mapped function). Other keys should go into a buffer to be returned when enter is pressed (i.e. act like the input built in). Finally I would like this to be cross platform and run on the command line.
Effectively I would like to be able to scroll a table down a page when the page down key is entered and still return the text from the input line when enter is hit.
I have found any number of solutions that don't fit all of these criteria:
input builtin - doesn't detect special keys and doesn't return until enter entered
curses - not shipped with Windows
msvcrt.getch - Windows only
tkinter - doesn't run on command line
readline - can't remap the standard navigation keys (such as home and end). When I bind these keys it still performs the vi or emacs navigation function
Any help would be appreciated.
You can download the keyboard module in python
pip3 install keyboard
You can then code as
import keyboard #Using module keyboard
while True: #making a loop
try: #used try so that if user pressed other than the given key error will not be shown
if keyboard.is_pressed('a'): #if key 'a' is pressed
print('You Pressed A Key!')
break #finishing the loop
else:
pass
except:
break #if user pressed other than the given key the loop will
break

Pressing keys with python win32api

I am trying to achieve the following behavior in a PYTHON 2.4 script, here are the steps, and after them, the question:
Python script starts
The script gives a 3 seconds delay to change to 'Z' program's window
The script does some clicks on the 'Z' program's window.
The script stops making clicks
/* ¿? */
Ask to continue with the program's excecution
/* ¿? */
Go to step 2
So, in steps 5 and 7 what I want to do is simulate the pressing of keys Alt+Tab in order to go back to the script window (in step 5), and go back again to the 'Z' program's window (in step 7).
And the problem is that I have no idea how to achieve this (the simulation to press keys alt+tab), and didn't find answers to my doubts.
I am using the python win32api modules to positionate mouse in a certain point and make the clicks, but I don't find the way to simulate the key pressing.
Try This :
1) Use : https://gist.github.com/chriskiehl/2906125
2)
import win32api
import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
shell.Run("app")
win32api.Sleep(100)
shell.AppActivate("myApp")
win32api.Sleep(100)
shell.SendKeys("name")
win32api.Sleep(500)
shell.SendKeys("{ENTER}")
win32api.Sleep(2500)
shell.SendKeys("^a") # CTRL+A may "select all" depending on which window's focused
shell.SendKeys("{DELETE}") # Delete selected text? Depends on context. :P
shell.SendKeys("{TAB}") #Press tab... to change focus or whatever
You need the WinApi function SendInput.
See the description in the MSDN:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310(v=vs.85).aspx
An Easier Way
Use this Library W32S
My Library.
And If u want , just copy the source instead

printing key presses to the screen instantly with python

I know this question is long but what I really want to know is in bold.
I would prefer to use python on Linux.
I'm trying to make a new keyboard layout kind of like devorak but the layout is set to either layout1 or layout2 depending on if you are holding a hot key or not (the hot key should probably be ctrl?)
e.g. press d -> "z" prints to the screen using key layout1
e.g. press ctrl d -> "x" prints to the screen using key layout2
My main problem (and question that needs answering) is the way characters need to print to the screen.
if someone presses the keys (in this order) "(a)(b)(c)(d)(ctrl+d)(shift+e=E)(f)(Enter)"
now lets say the output for these key presses should be "oijzxKb"
I don't want output to render with new lines:
o
i
j
z
x
K
b
I want the characters to appear instantly on the screen as each character is pressed (without waiting for them to press enter).
e.g.
press o
Screenshot1 o
press i
Screenshot2 oi
press j
Screenshot3 oij
.. etc
I assume I will need the following:
a way to read keypresses instantly
a way to print key presses instantly (to the terminal or a GUI or whatever is easiest initially, if it worked on any editor that would be cool!)
I could probably do this in PyGame (but then I probably wouldn't be able to cut and paste etc) and I'm guessing there should be an easier way.
I'm using a Logitech G110 keyboard, I may eventually want to use this as an alternative to my qwerty keyboard on all my applications across all my devices.
Thanks!
EDIT: SOLUTION:
Thanks to the first response,
using Getch from http://code.activestate.com/recipes/134892/
getch = _Getch()
word=""
while True:
c=getch.impl()
if c=="a":
word+="z"
elif ord(c)==127: #backspace
word=word[:-1]
else:
word+=c
print word
This will suffice for now thank you. Once I'm happy with refinement I'll look at doing something lower level, operating system specific without python.
One problem with getch however is that ctrl+a cant be distinguished between ctrl+A (e.g. if you hold ctrl and press keys, it can't tell the difference between upper and lower case)
If it's ok to depends on the X window system, you can use the python-xlib module or the xpyb module to access the X window system and use a XGrabKey call to grab the keyboard related events. Upon each KeyPress event you will be able to print the pressed key.
Now, if you really want to write a keymap, this is totally OS/window system dependent. If you use the X window system (Ubuntu does), you need to check the X documentation about how to write a new keymap. On Ubuntu, the current keymaps definition should be in /usr/share/X11/xkb. Take a look, and try to copy and edit one. You can use setxkbmap to change the current keymap then.
To modify the key mapping of your keyboard, you must use the tools provided by your OS. Most applications don't accept generated events for security reasons.
In your case, that would be xmodmap. Don't forget to create a backup of your current keymap using the -pke option because you will make a mistake - and then, your keyboard won't be working anymore.
If you also want your new keymap work on the console, have a look at the kbd package which changes the keyboard layout at the kernel level.

Categories

Resources