I have a problem, that consists in following: Pyautogui typewrite won't type letters, only numbers. For example, when I execute
pyautogui.typewrite("abc123")
only "123" appears.
This question is similar to this one:
Pyautogui typewrite is writing only numbers
Unfortunately, there are no answers about the issue, as well as other Internet topics.
I have the Windows 7 machine and Python 3.5.
There seems to be a bug in the typewrite function of PyAutoGui. I workaround it with this function which preprocesses the string into keypresses instead. Note that this version of the function leaves a comma at the end of the output array so that you can easily append more characters or button presses at the end.
def preprocess(something):
something = str(something)
output = []
for x in range(len(something)):
output.append(something[x])
output.append(',')
return output
I faced the same problem. I was not able to send letters using typewrite() function.This bug in PyAutoGUI can be overcome by installing OpenCV 3.1.0
Download openCV 3.1.0 from below site:
1) https://sourceforge.net/projects/opencvlibrary/files/opencv-win/3.1.0/opencv-
3.1.0.exe/download
2) Extract the OpenCV -> Then go to OpenCV\build\python\2.7\x64 and copy cv2.pyd to C:\Python27\Lib\Site-packages
And your problem will be solved. Give it a try, it worked for me
I had the same problem couple of days ago
Try using other pyautogui functions that presses keyboard keys like pyautogui.press("k")
If you have a problem with all keyboard functions related to letters in general it's probably because your default input language is set to something other than english
you can fix that easily by going to your keyboard and input settings and changing the default input language to english
you'll still be able to write in your first language and it will hopefully solve your problem
Related
Good morning,
I am using Python's 3.8 pynput (in windows 10) to get each time the character entered (in system level) and then the unicode of this character.
from pynput import keyboard
def on_press(key):
if key == keyboard.Key.esc: #if button escape is pressed close the program
listener.stop()
else: #if button escape is not pressed get the unicode code of the button-char pressed
unicode_code = ord(getattr(key, 'char', '0'))
print("The unicode is ",unicode_code)
print("The char entered is",chr(unicode_code))
controller = keyboard.Controller()
# Collect events until released
with keyboard.Listener(on_press=on_press) as listener:
listener.join()
The problem I am facing is that when I change my keyboard to greek (with shift+alt), it keeps the unicodes of the english language and not the greek ones.
You can see the screenshot attached to understand better.
What to do to overcome this problem?
No, you aren't getting English unicode codes. You are getting unicode representation of codes returned by the keyboard. They will be the same no matter what language is chosen because keyboard layer is designed to always return same codes for same keys (by their location and/or meaning on querty/quertz or other HID compliant keyboards) if possible. What you are doing in your code is essentially reverting the process done by pynput which converts the keyboard code using char()/unichar() function. Think about it: is there a greek representation of e.g. F11 key?
I cannot remember whether pynput has higher-level inputs support or not, although I think it does. and that it should be possible to get directly what you want
What you have to do is either to find another attribute (if there is one, I am not pynput specialist) containing the character sent to the input field, or check the current keyboard language and appropriately map the codes returned. You can also try playing with codepages using the codecs module. Worst case scenario would include snatching the character directly from the GUI's input field. But that would be very inelegant, bruteforc-ish and simply the shouldn't do it thing if not strictly necessary. There are other methods for getting input from the OS - like directly linking to its event input system or kernel using built-in libraries through ctypes, wintypes, Cocoa/Carbon on Mac or GTK on Linux.
Try using pynput version 1.1.7, it works there. It is broken after 1.4 and in between these two it works, but changing the layout with the windows shortcut while the program is running, isn't working 70% of the time, meaning Windows just doesn't change the language. Alternatively, you can work around it in newer verisons by trying this solution, that converts the characters in real time if you don't care about performance. I have opened an issue for it to be fixed in the repo's github.
I want to detect if Ctrl + X is clicked so that I can exit my Python application. I don't know how to do it? Please help.
Have you thought about using a function like the ones discussed here? Python read a single character from the user
Or maybe you could use curses.
Either way you would just need to find the key code for ctrl-X, it's 24.
The simple and the best option is to use the module keyboard. Install it using pip install keyboard.
Use the following code at the start of your code:
import keyboard as k
k.add_hotkey("ctrl+x",lambda: quit())
#Your code....
Well, it works easily, but, it will read keys from the whole windows. For example the program is running and you are using notepad currently and you pressed ctrl+x , then the python program will close too.
I have a laser pointer that I'm using along with my webcam as a drawing tablet, but I'd like to use the extra buttons on it too. They seem to be bound to Page Up, Page Down, Escape, and Period. I can't seem to figure out a way to get the input(which is handled like it's a keyboard) without any windows being selected.
I've tried serial and pyusb, but I've had issues with both of those. I got it to work with Pygame, but as far as I know, you can't receive input without the window it creates being selected. Any ideas?
You could try making a python key-logger. However, it would be much easier to just use Pygame.
CodeSurgeon answered me in a comment.
Looks like there are a lot of youtube tutorials on the subject, surprisingly. This one shows a cross-platform approach using the pynput module, while this one looks to be using a windows-specific approach (pyhook and pythoncom). Can't vouch for either of these as I just found them through some searching, and I am sure there are others as well.
I found that pynput works for me. (Windows 10/Python 3.4)
My keyboard contains a row of buttons for various non-standard keyboard tasks. These keys contain such functions as modifying the volume, playing or pausing, and skipping tracks. How can I simulate a basic play/pause with Python? I am on Windows, by the way.
I would use pywin32. Bundled with the installation is a large number of API-docs (usually placed at something like C:\Python32\Lib\site-packages.) It essentially wraps a lot of stuff in the Win32-library which is used for many low-levels tasks in Windows.
After installing it you could use the wrapper for keybd_event.
You could also use SendInput instead of keybd_event but it doesn't seem to be wrapped by PyWin32. SendMessage is also an option but more cumbersome.
You'll need to look up the virtual scan code for those special buttons, since I doubt the char-to-code mapping functions will help you here. You can find the reference here.
Then it is a simple matter of calling the function. The snippet below pauses Chuck Berry on my computer.
>>> import win32api
>>> VK_MEDIA_PLAY_PAUSE = 0xB3
>>> hwcode = win32api.MapVirtualKey(VK_MEDIA_PLAY_PAUSE, 0)
>>> hwcode
34
>>> win32api.keybd_event(VK_MEDIA_PLAY_PAUSE, hwcode)
MapVirtualKey gives us the hardware scan code which keybd_event needs (or more likely, the keyboard driver.)
Note that all this is snapped up by the keyboard driver, so you don't really have any control where the keystrokes are sent. With SendMessage you can send them to a specific window. It usually doesn't matter with media keys since those are intercepted by music players and such.
This was not working for me on Windows 10 64, all recent updates installed. I needed this (the 3rd parameter 2) before it'd work.
win32api.keybd_event(0xB3,0,2,0)
didn't bother looking into why it works, but threw it out there since this and other similar posts had me frustrated as for some reason not working on my PC.
This successfully paused/played Amazon Music on my PC.
You can use pyautogui. This library contains a lot of keyboard and mouse simulations.
To install run pip install pyautogui in cmd.
In order to simulate a play/pause keypress, you should use pyautogui.press("playpause").
Check out their docs at https://pyautogui.readthedocs.io/en/latest/keyboard.html to see the list of the supported keys and some other keyboard functions.
I can't even copy and paste a strange checkmark looking character from my IDLE interpreter session into this text field...I am running Linux Mint Julia, python version 2.6, but it also happens in 3.1. This occurs after holding the shift key and pressing backspace.
I tried doing this:
>>> ord('[strange-checkmark]')
8
Then running the reverse:
>>> chr(8)
\x08'
I'd really like to get this strange character to stop printing, and just delete the previous character instead.
I tried to recreate this character in gedit, terminal, and chrome. No luck. Searching for a ASCII chart of ordinal values calls this character backspace.
Here's an image from my session:
It's a confirmed bug within Tkinter, and not necessarily IDLE. See Issue 1482122.
In brief: If you want to fix it, you're welcome to try xmodmap -e "keycode 22 = BackSpace", but you run the risk of no longer being able to issue a Ctrl+Alt+Backspace to kill your X Server.
I can keep looking for more information into the bug; I'm not sure if the folks working on Tkinter have resolved this yet.
EDIT: Confirmed second source on issue - See Linux%Shift-Backspace on the Tkinter Wiki