Live Keyboard Input in Python? - python

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)

Related

Window resolution in Tkinter

I know that this might sound weird, but I have a problem considering the screen resolution of my Tkinter program. I have attached two pictures for a comparison.
I think you can clearly tell, what I'm talking about. The problem is that I don't really know why this happened. Suddenly the tkinter window had a way better resolution as you can see in the top picture. I did not change anything related to screensize/resolution by myself and was therefore expecting Tkinter to have published an update which just has better textures for every widget.
But because I had to reinstall python and all its modules this is no longer the case and the widgets look very blurry again (Bottom picture). Now I'm asking myself whether this has something to do with another version of Python or tkinter I'm using or what else could have been the reason for that.
I did not change my PC, Screen or anything like that. I just reinstalled python and its modules. Sadly I don't have the source code of the version where it looked great, but only an .exe file, because im continuously updating my program. But as I said before, I don't know what I could have changed which led to this huge resolution improvement.
I hope you get, what I'm trying to tell you.
Thanks in advance!
I was able so solve it!
I had a look on this question: on Stackoverflow
The answer from #binaryfunt solved my problem completely. So it was not really a tkinter/python thing, but rather windows itself.

Capture keyPressEvent in minimized PyQt application

I am trying to write an error reporting application and would like to keep it as a tray icon for the most part - but the idea is to maximize it with a specific key combination (e.g. "Ctrl+Alt+H", or something like it).
Look like not possible as is. But I found that. It should be a start of an answer for you:
http://libqxt.bitbucket.org/doc/tip/qxtglobalshortcut.html
How to detect global key sequence press in Qt?
Thanks for reply, I was looking deeper into some of the codes I had in my repo and found out this -
https://sourceforge.net/p/pyhook/wiki/PyHook_Tutorial/
This is specifically for windows but saw pyxhook for linux
http://jeffhoogland.blogspot.in/2014/10/pyhook-for-linux-with-pyxhook.html
Tried the windows version and it is working like a charm, need to customise that though. Have to the linux version as well

How to make VLC click through

I have been all over the web looking for a solution to this problem!
I'm looking for a way to make my VLC window "click through" on linux.
I have found some python code here: https://stackoverflow.com/a/11917174/3426514 that says it can solve the problem, but I dont know how to apply that answer to my VLC window.
Is there a way to make a bash script that i can execute after vlc has started that will make the vlc window click-through? Or anything else, like a CCM plugin or a vlc plugin or magic foo?
I'm on ubuntu 14.04 if that is helpful :)
Edit: Another implemetation, part of the notifications code:
https://github.com/dkasak/notify-osd-customizable/blob/master/src/bubble.c#L1608
Edit2: A SVG Implementation, dont think this applies, but it is kinda relevant https://superuser.com/questions/699289/making-cairo-clock-click-through-able-cairo-graphics
Edit3: The used to be a compiz plugin called ghost a long long time ago https://github.com/compiz-reloaded/compiz-plugins-experimental
Edit4: To address the downvote to close: I expect the actual code to do this would be a few lines at most, i just have no clue how to do it. If there is something that I can provide to narrow the focus or provide more information, I will gladly do so.
Edit 5: Another related question. Create a click through window for Linux
You can have VLC write to the desktop background, then set all your app windows to be partially transparent. Output to Background is a VLC option. What you want is backwards from that, where VLC is overlayed on top of everything else, but ignored for user input, however if you don't mind going the other way, it's "already in there" 8-)

Background python program inspect GUI events

I am thinking of writing a python program that runs in the background and can inspect user's GUI events.
My requirements is very simple:
1) When user right click the mouse, it can show an option; and when this option is chosen, my program should know this event.
2) When user select a file and click some predefined key combination, my program should know this event.
What should I do? Is this a GUI program? I am also thinking that, this program maybe a daemon on the machine and can inspect the user's GUI event, but I am not sure how can I do this.
Thanks.
If you're talking about doing this stuff inside of a wxPython program, then it's all pretty simple. There's a PopupMenu widget for the first one and an AcceratorTable for the second one. If you're wanting to catch mouse and keyboard events outside of a wxPython program, then you have to go very low-level and hook into the OS itself, which means that there really isn't any good way to do it cross-platform. You'll probably want to look at ctypes and similar libraries for that sort of thing.
I've been researching this problem a bit and while I don't have a definite answer, I thought it would be good to share what I found.
First, according to the answers for this question, wxPython cannot track nor control the mouse position outside of the area controlled by the wxPython app. However, as we can see in the answers to this question, under certain circumstances it may be possible to create system-wide hot keys that wxPython can see (although we can also see that this is probably not the optimal solution).
So what will work for the OP? Well, build a cross platform application that does what the OP wants seems almost impossible so I will assume the OP is developing for MS Windows. Following the suggestions in those previous two questions, I looked into the PyWin32 API. It's a little hard to find documentation for it but this question offers some helpful links. A little more digging and I found this tutorial for system-wide hotkeys as well as the win32event.WaitForMultipleObjects and win32event.WaitForSingleObject methods which, if the OP can figure out the proper event, may be what the OP needs to catch menu clicks.
I know it's far from a complete answer but hopefully this can serve as a good jumping-off point for further research.

Media Play/Pause Simulation

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.

Categories

Resources