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.
Related
I want my configuration menu to open at the start of a script if the Ctrl key is being pressed. That is, the user would start pressing the key before the script starts running.
To intercept that I have tried keyboard.is_pressed('ctrl'), but it always returns false.
I have also tried keyboard.read_key but this method gives me two problems:
If it is not pressed, the program does not continue running.
If it is pressed, it just continues running only when I release the control key.
Maybe I just need to think of another way to open this menu.
I'm assuming you're using the keyboard module for this. Looking through the code, it seems like that library is event based, so if the CTRL key is pressed before your program starts, then it isn't recognized by the program.
My recommendation would be to read directly from stdin using the sys library. However, looking at How to get Ctrl, Shift or Alt with getch() ncurses?, it seems like CTRL, ALT, and SHIFT don't generate their own input to stdin but rather modify existing input, so you should probably look at using another key as your modifier. You also need a timeout, which Read file with timeout in Python suggests the select module provides. In case you don't know already, sys.stdin is your standard input, and you can treat it like any other file.
Assuming my code is going to run on Windows and thanks to my experience in C++ I came up with the win32api module.
# Windows module to controll the keyboard
import win32api
# Windows module with every key code
import win32con
# Check whether the control key is pressed
if win32api.GetAsyncKeyState(win32con.VK_CONTROL) & 0x8000 > 0:
# Do stuff
I have a program in Python that starts another executable. Some automated operations need to be done in the ribbon of this open executable, so I use pyautogui to do so.
First the ribbon needs to be ‘active’, so I click on the left most part.
Then I need to use the arrows to change the ribbon menu selection (two times to the left).
Then I need to hit enter to open the correct menu. (going from 'File' to 'Scripting')
The code I’m using for this is:
import pyautogui
pyautogui.click(x=0, y=30)
pyautogui.press(['left', 'left']) #this part does not work here
pyautogui.hotkey('enter')
Somehow, the click and enter do work, but the arrow-keys don’t work. I can use the physical arrow-keys to change the menu selection, but this code doesn’t perform these actions somehow.
Does someone know what is wrong here and how to solve this?
Best regards,
Ganesh
EDIT:
I tried to open both the program and the script with admin right, but that still didn't work. Somehow, the 'enter' and everything else works, except for the arrows.
Ganesh, it might not work with pyautogui as the program/ interface you're using might simply not register the key. To use the arrow keys or other special keys like 'enter' I would suggest using pydirectinput
First, install the library if not already
pip install pydirectinput
Then you can rewrite your code as
import pyautogui
import pydirectinput
pyautogui.click(x=0, y=30)
pydirectinput.press(['left', 'left']) #this is the updated line of code
pyautogui.hotkey('enter')
So I've been attempting to get a script to type out a string of text in a video game (Guild Wars 2). Mainly I'm using pyautogui and for the most part it works fine. My issue is it seems I can't get the game to recognize 'enter'. For example if I have the code:
import pyautogui, time
time.sleep(2) #to allow me to have time to switch windows
pyautogui.press('enter')
pyautogui.typewrite("This is a test")
pyautogui.press('enter')
The two "press enter" function will not open and submit the text. If however I manually hit enter, the 3rd line types things out just fine.
I've also tried replacing press('enter') with keyDown followed by keyUp, but with still no results.
I've managed to create a workaround by having python hit F10, and then a separate Autohotkey script hitting enter when F10 is hit, but that is far from ideal. Are there any suggestions?
Extra note from comments: The script by itself works fine in other programs such as notepad. It seems to fail exclusively for the game client.
For anyone finding their way to this post. The answer is essentially that this can't be done in this fashion due to how most games interpret keypresses. A working system is shown here: Simulate Python keypresses for controlling a game
None of \n nor return works to me, so I have to use pydirectinput: https://github.com/learncodebygaming/pydirectinput
Install it using pip install pydirectinput, then import it and use as the README. Note that the file also needs to run as admin.
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
Is there a way to simulate a long keypress in python?
I tried with SendKies and win32api, with no luck. I would like to keep using python
2.5 or 2.6, because i also need the pybluez module. Thank you in advance.
Something I have done to simulate a longer key press is use a time.sleep(amountoftime) between the key press and key release. Here is a site that shows an example of this.
http://www.python-forum.org/pythonforum/viewtopic.php?f=18&t=9513