Simulating a long keypress in python - python

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

Related

How to bind keyboard keys to clicks on certain positions on screen Mac

I've been trying to bind my up, down, left and right keys on my keyboard to certain clicks on my screen. I am an absolute noob at Python so I'm really struggling to get it done but it seems like a nice little project to get better at coding.
I was wondering if anybody has some general tips on what libraries, etc. I should be using for this task?
I am by the way on a Mac laptop if that matters.
Thanks in advance!
Use the 'pynput' module to read keypresses and map them to the functions you want to create using an if statement.
Looks like you want to create hotkeys, refer to this video for a detailed explanation: https://www.youtube.com/watch?v=n_dfv5DLCGI

Python simulate media keys

How to make Python (3.7) manipulate any media player currently working on Windows?
I want to get functionality similar to media keys on keyboard, for example:
play_pause.py script which will play or pause music on Spotify or movie in media player (whatever is currently playing).
play_next.py script which will play next song/movie etc.
To clarify: I don't want Python to virtually press actual media keys on keyboard. I would like to get the functionality of such keys so it might work even without keyboard connected to PC.
The easiest solution is to use win32api.keybd_event from pywin32.
For example, install pywin32:
pip install pywin32
And try play/pause - should work without keyboard:
import win32api
from win32con import VK_MEDIA_PLAY_PAUSE, KEYEVENTF_EXTENDEDKEY
win32api.keybd_event(VK_MEDIA_PLAY_PAUSE, 0, KEYEVENTF_EXTENDEDKEY, 0)
Virtual-Key Codes: here and here.
NOTE:
At this link about keybd_event function, you can see message: "Note This function has been superseded. Use SendInput instead".
So if you want/need to use SendInput, you probably need to use ctypes. I suggest you to check the example here. I've tried that code too and it works. If you need any further help, let me know.

Python Console: Detect Ctrl X Key Press Event

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.

Python 3, capturing key combinations

I am writing long running program with simple GUI, the 99% of time I would like the program to work only as process, however sometimes I want to check the status, so is it possible to capture the keypress event in python?
For example I want to show the program window when I press Ctrl+Shift+Alt+Q, I expect to use app on Windows
Thank you
There are tutorials on how to create a key-logger with Python. They should help. But I do not know if that is the right way to go.
Also you could register shortcuts under a key combination on Windows.
You should be aware that Ctrl+Shift+Alt are handled independent of the keyboard layout and Q changes with the language.
With pywin32 you should be able to do that using Ctrl+Shift+Alt+F1 for example.

Python 3.x Interaction with other Program GUIs

I'm looking for a Python 3.x library that is able to allow interaction with other programs.
For example, I already have some sort of command-line interface which I have developed in python, and I
want to be able to enter, say "1", and have another program open. From here, I wish to hit another
input like "2" and have it manipulate the GUI that opens (for example, for it to "click" the Configurations
dropdown bar and select an option, perhaps modify a few settings, apply, and then possibly also automatically
enter some text). The reason I'm doing this is for test automation.
I've already tried using pywinauto, but I've found it to not be compatible for Python 3! :(
Is there another possible approach to this? Thanks in advance!!!
P.S. I may have forgotten to mention that I'm using Windows 7 but with Python32
You could look into sikuli. It lets you automate clicks and other actions based on region or matched graphic. Fairly smart. Is there a reason you're dead set on using py3?
Py3-compatible pywinauto released! New home page: http://pywinauto.github.io/
P.S. I'm maintainer of pywinauto.
Late answer, but have a look at pyautogui which enables you to move the mouse and press keys. I used it for the following snippet which launches an emulator and presses keys.
import pyautogui as pg
import os
import time
game_filepath = "../games/BalloonFight.zip"
os.system(f"fceux {game_filepath} &")
time.sleep(1)
keys_to_press = ['s', 's', 'enter']
for key_to_press in keys_to_press:
pg.keyDown(key_to_press)
pg.keyUp(key_to_press)
time.sleep(2)
im = pg.screenshot("./test.png", region=(0,0, 300, 400))
print(im)
A more detailed expalanation can be found here: Reinforcement learning to play Nintendo NES games
I created a pywinauto fork on GitHub that's compatible with Python 3:
https://github.com/Usonaki/sendkeys-py-si-python3
I only did basic testing, so there might still be some circular import related problems that I haven't found.

Categories

Resources