I am trying to do automatic scene changer in OBS by having a python script search for a specific image on screen. When the image is detected, the python script will send the hotkey, which should be picked by OBS (Same hotkey added in program).
However, I tried this in various applications like chrome/notepad/some games/etc and the hotkeys DO get "transmitted", but OBS doesn't pick them up. I don't know why, any help ?
From this thread: Key Presses in Python
It seems like you need to install pywin32 module.
With that you can send key presses to other applications:
import win32com.client as comclt
wsh= comclt.Dispatch("WScript.Shell")
wsh.AppActivate("Notepad") # select another application
wsh.SendKeys("a") # send the keys you want
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 am using Linux Mint 20 with Python 3. I am a complete noob with Python. However, I am trying to set up a keyboard shortcut to display certain text wherever my cursor is at in the active window (an input box, text editor, etc.). I know how to set up shortcuts and run simple python programs in Terminal.
It seems I have to use some type of GUI interface? I have looked at GTK and pkinter, but I can't seem to figure out the correct code. Would rather use GTK or something that already comes with the system.
Thanks.
You can use the pynput module to make keypresses wherever the cursor is. Here is an example:
import pynput
from pynput import keyboard
string_to_type = "Hello! How are you?"
c = keyboard.Controller()
for char in string_to_type:
c.tap(char)
c.tap() presses and then quickly unpresses the key passed as an argument.
You can also call c.press() and c.unpress(), both of which require an argument representing a key. This enables you to hold down keys.
Is there a way to start a python code automatically when a set of keyboard presses are made?
context:
I want to be able to hit control v (or any other custom key binds) inside a specific folder and the script will run which will result in the picture that I have stored in my clipboard will be automatically made to a jpg.
this is the script that I am using to convert the picture in my clipboard to a png
from PIL import ImageGrab
from datetime import datetime
import os
im = ImageGrab.grabclipboard()
date_now = datetime.now().strftime('%c')
im.save(f'{os.path.dirname(os.path.abspath(__file__))}/{str(date_now)}.png','PNG')
P.S. this is because I am tired of getting a screenshot and then needing to open paint to just image and saving it.
You can do it using another python script that runs all the time:
$pip install keyboard
import keyboard as k
import os
# function to run your python script after keyboard shortcut
def run_script(path_to_file):
os.system("cmd /k python " + path_to_file)
#add hot key for you shortcut
k.add_hotkey('ctrl+shift+a', run_script, args=["enter your path to file here"])
k.wait()
Or else if you are using Windows, create a shortcut for your python script (right click it and then "Create shortcut"), right click the shortcut file you just created and click "Properties", then go to the "Shortcut" tab and click the "Shortcut key:" box, and finally type the combination that you want. if it seems like its not working, than its probably because the python file has no wait on the end of it, so it executes but then immediately closes.
i think this is related to the operating system or window manager you use. For example: in XFCE you'll find it under applications / settings / keyboard / shortcuts. (or something like that.. my desktop is held in German language..)
under Windows you can make a desktop link to your software. When you edit it you can set a shortcut for running this, too.
try to ask google for something like
[your os] shortcut for program
I was thinking of coding a script that will run in the background of my computer that will detect key combinations to start specific tasks.
For example, the user presses Ctrl+Alt+F1 then the browser starts. However, I cannot seem to find a way to detect these combinations while the script is running in the background.
I thought of using a .pyw extension for my script, but that makes the script not interact-able.
If anyone needs to know, my OS is Windows 10.
I think the pyHook library is what you're looking for:
The pyHook package provides callbacks for global mouse and keyboard events in Windows. Python applications register event handlers for user input events such as left mouse down, left mouse up, key down, etc. and set the keyboard and/or mouse hook.
For a personal project, I want to generate keyboard presses for a N64 emulator. It seems like it recognizes only one keyboard event when I select the Python IDE then switch to the emulator. I want the emulator to constantly recognize the button presses the python script generates. The code is really simple, it constantly replicates the x button being pressed.
import serial,win32api, win32con,time
while 1:
win32api.keybd_event(0x58,0x2D,0,0)
time.sleep(1)