Python Keyboard shortcuts - python

So, I have a python script running I the background, and I want it to trigger X function whenever a key combination is pressed. How would I implement that? The problem is that the app will be running in the background, so it won't have focus from the OS.

Well, it basicly has nothing to do with python. That heavily depends on the operating system you're working on.
You can use XLib under Linux (http://python-xlib.sourceforge.net/) or wxPython under Windows (http://wxpython.org/docs/api/wx.Window-class.html#RegisterHotKey)
Another idea would be to hook the system keyboard events (through pyHook) and try to catch any hotkeys.

Related

Python: detect keyboard before other processes

I wrote a Python script that performs simple actions based on what I press on my keyboard.
The script always works perfectly, except when Visual Studio Code is open and active.
It's like VSCode is catching the pressed keys BEFORE my python script.
If I close VSCode, or just minimize the window, and another window is active, my python script works again.
VSCode does not "steal" the keys only from python scripts, it steals them from other applications as well. For example, when VSCode window is active, I can not use my OBS shortcut for start recording.
I tried to lower VSCode priority and increase my python script priority, but it did not work.
Does anyone know how can I make my python script catch the pressed keys, before VSCode steals them?
EDIT:
Please find below a minimum reproducible example.
The following script prints an a when the a key is pressed on the keyboard. It works with any active window, except VSCode. In fact, when VSCode window is active it stops working.
Tests done in Windows 10.
from keyboard import is_pressed
from time import sleep
while True:
if is_pressed('a'):
print('a')
sleep(0.2)
I have found the solution!
I noticed that my VSCode was set to always run as administrator. I set this option months ago, and somehow persisted even after completely uninstalling and reinstalling VSCode.
I just disabled the option and now it works!
Thank you ColdFish and MingJie for all of your help.
Thanks for the detail Jeffrey. I could not reproduce the issue on a reasonably fresh install of VSCode, also on Windows 10.
I looked through the documentation in the readme of the keyboard package and found this in the "Known Limitations" section:
Other applications, such as some games, may register hooks that swallow all key events. In this case keyboard will be unable to report events.
I'm going to hazard a guess that one of your VSCode plugins is registering a hook to capture key events. I suspect that if you try it on a fresh VSCode installation (which will be similar to mine) it may work.
Alternatively, you can try using another similar implementation in pynput of the same functionality. That may work without any alterations to VSCode. A minimum example is here that will mirror the functionality of your minimum example:
from pynput.keyboard import Key, Listener, KeyCode
def on_press(key):
if key == KeyCode.from_char('a'):
print('{0} pressed'.format(key))
while True:
with Listener(on_press=on_press) as listener:
listener.join()
You can find further documentation on how to handle the keyboard with pynput here.

Is there a way to grab/trap all keyboard events using Python and GTK?

I am trying to write a program in python that behaves like a screensaver or screenlock.
I can prevent the interactive user from interacting with other running applications by calling gtk.gdk.keyboard_grab and gtk.gdk.pointer_grab.
However, the user can still press ctrl-alt-F1 to F7 to switch to the console. The user can also press ctrl-alt-esc to run xkill or press ctrl-alt-backspace to restart X.
Is there a way, using only python and GTK (pygtk), to prevent users from doing this?
The application is primarily targeted to run on linux mate.

Python interacting with OS X - is it possible?

I'll explain my question: is it possible to write a Python script which interacts with OS X architecture in a high-level way?
For example, can I gain control on Mac OS X windows resizing from a Python script? Are there modules for that? I'm not finding any.
To push things even further, would I be able to control keyboard shortcuts too? I mean, with Python, could I write a script that opens a terminal window everytime I type cmd + Enter from wherever I am in that moment, as if it was a system shortcut (Awesome WM style, if you know what I'm talking about)?
Hope I've been clear.
The 2nd one you can't do for sure, since the events are grabbed by other processes.
You should look for a osx specific library for doing that and then write a python wrapper around it.

Minimize python to system tray in Windows (Vista)

I would like to know if it is possible to minimise my python program to the system tray.
Basically what it does is at every hour it takes a screenshot but I don't want it to stay on the task bar taking space. Could I make it go to the system tray area next to the clock but keep it running?
I am not using tkinter or anything like that.
Since you are running on Windows, you may simply want to rename your script to have a .pyw extension, so there is no console window. If you try to make a system tray application, you will need to pick a GUI toolkit like you've suggested, and your simple script will become a LOT bigger and far more complicated.
Seems like some rather helpful folk here...
I was curious myself, and am poring over this persons PyWin32 app which might help your cause, Matthew:
http://www.brunningonline.net/simon/blog/archives/SysTrayIcon.py.html
As opposed to having the program running continuously, why don't you use your system's scheduler? (Cron under *nix, Task Scheduler under windows). There might be a bit more overhead since it has to spin up Python each time, but it would be easier than hooking up a notification icon.
Since you refer to it as the task bar, I assume Windows. To have a notification icon, you would have to have a hidden window, with a running message pump, so Windows has somewhere to send messages to for the icon.
So, in short, much simpler just running a scheduled job.

How do I register global system keyboard hotkeys on Mac OS X in wxPython/Python 2.7?

I'm writing a GUI-based app in Python 2.7 using wxPython 2.8. I'm trying to run everything on a Mac, and unfortunately it seems that wxPython's wx.Window.RegisterHotKey() method only works on Windows. I would like to be able to set a global hotkey or key combination while the application is running, and have that key combination get passed on to the application itself, even when it doesn't have focus. How can I allow my application to be notified when a (user-defined) global hotkey/combination is pressed on Mac OS X, even when the app doesn't have focus?
When the app has focus, you should be able to use wxPython's AcceleratorTable:
http://www.blog.pythonlibrary.org/2008/07/02/wxpython-working-with-menus-toolbars-and-accelerators/
http://www.blog.pythonlibrary.org/2010/12/02/wxpython-keyboard-shortcuts-accelerators/
I don't have a Mac, so for you'll have to try asking on the wxPython mailing list or the wx-mac mailing list for help about registering a hotkey outside of wx.
This is currently not available on platforms other than Win32. I have an open ticket for it on the WX tracker:
http://trac.wxwidgets.org/ticket/12354
You'll find a link to how to accomplish this with Objective C, and you should be able to do it with PyObjC, but both WXPython and the outlined method for capturing hotkeys on OSX require running in an event loop in the main thread.

Categories

Resources