Raspberry Pi python kayboard background listener - python

I am currently trying to create a python script that will listen for a keypress, specifically numpad along with the Numlock, / , * ect. The script will eventually be added to a shared memory script I wrote and will be run as a daemon.
I need this listener to be independent of of any open window or focus. A key logger if you will, so no matter what i am doing if i press a key it will be picked up by my script.
The trouble i am having at the moment is poor documentation of most things i find and python is not my main language. I have found pygame, termios and some others but i cant seem to find a straight forward solution.
So basically my question is, how can i capture a keypress regardless of the current active window?
Library recomendations, code or anything would be a massive help as i have been stuck on this for quite some time.
Thank you.

Related

How to make python program that runs game files at the same time as main code

So I'm very new to Python so I'm not sure of the correct way to do what I need.
Basically I am creating a laser tag game. The whole game is controlled by some python code that runs on a PC, the hardware is basically just inputs and outputs for this code.
I want to have game files that are simple files with just the code for the game itself, that way it is fairly easy for anyone to make their own game modes.
Then there needs to be a main program with a GUI (Probably Tkinter) and code to handle sending/receiving information from the hardware (Laser tag guns). I need some way to select a game file from the GUI and run it, but I still need the main server code to be running (to take care of sending and receiving information from the guns, displaying live scores on the GUI, etc).
What would be the best (preferably fairly simple) way to go about doing something like this? Thanks!
Cool project! Generally you let TkInter run in the main thread, and have the other tasks executed in other threads or processes. Brace for some hacking, parallelism is unfortunately pretty difficult to do right.

How do I get a value asynchronously while not blocking the UI in PyGTK

good people of the internet.
I'm writing a GUI application that needs to get the ID from an RFID card scanner attached to a Raspberry Pi. I'm using PyGTK in combination with Glade for the UI.
When the user clicks on the 'confirm' button, I want the app to scan for any cards (I have a function with a while True loop for this), but the user should also be able to press the 'cancel' and stop scanning for cards and reset some values.
I wanted to do this by starting the scanning function in another thread (or process) and, once scanned, make that process trigger a callback function to process the ID and run some other code, but I can't find any libraries that allow me to do this the way I imagined it could.
I've tried using Pool but calling pool.apply_async(RFID.Get, callback = self.OnScan).get(timeout=15) still blocks the IO.
I've done some async work in C# before this but am not at all completely familiar with how it's implemented in Python.
(The GUI I'm using.)
I need to get this done A.S.A.P., so any help, hints or clues are appreciated. If I need to supply any code I will (although this project is not open-sourced, so I can't share everything).
Thank all of you in advance.

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.

Get focus on tkinter program after pc is unlocked

I have a tkinter program written in python 3.3.3. I see myself in the need of making the the program get focus when the user unlocks the computer. I don't really know how to go ahead and start with this, users have a .exe version of the program that I created with cxfreeze. I f i need to modify the .py and create another .exe, that wouldn't be a problem.
After some research I found that one can use the ctypes module to lock the computer, but it's not very helpful because i need to know if it is locked or unlocked. I also saw commands from win32com, but i can't seem to be able to find a way to trigger a command when it gets unlocked.
What is the best way to get focus on my program after the computer is unlocked??
Any help is greatly appreciated.
I cannot answer this specifically for 'unlock' event (if there even is one in the GUI; I can't find one by cursory search.).
But I think the real answer is to change the question. Having a program simply take focus when user unlocks the display is very un-Windows-ish behavior. The Windows user expects to see the desktop just as s/he left it before the display was locked -- why does s/he want to see your program on top when unlocking, regardless of why Windows might have locked the display?
Maybe you want to recast your program as something that runs in the background and pops up a notification (in the notification area on the right side of toolbar) when it wants user to do something?

Is it possible to get current application running with python script

I am very much concerned about my productivity all the time. I have recently come across this beautiful chrome extension Limitless
But this is only measuring what i'm doing within the chrome application. As I work most of the time with pdfs, videos etc, I want to develop similar application for linux(ubuntu) desktop enviroment.
Basically I want the script to run continuously as long as the workstation is on.
It should be able to know what I'm currently looking at (for eg a pdf file or a lecture video in vlc) and get the name of the respective file, start time, end times etc and finally post to db.
It is better if it could know if the system is idle or at sleep.
I don't have slightest clue at bash scripting. so my questions is could this task be accomplished with python.
What I've tried?
I started with a search in google "get current application python", "current window title python" etc etc and really surprised to see absurd results.
Please give me pointers on this.
I think you are asking for vocabulary. So I give you what I know.
You are using Ubuntu so your Window Manager may be Gnome.
The window manager knows which window has the focus.
So maybe you want to find out which window has the focus and you want to map it to the Process that opened the window.
What you need to focus on is is module for Python or a Python Binding for the window manager. This module is likely to also be able to control the windows.
The window manager is started with startx.
You could try to call a command line tool and catch the results
How do get the process list on command line:
https://stackoverflow.com/questions/53489/how-do-you-list-all-processes-on-the-command-line-in-windows
And how to call a tool with python:
Python subprocess.call and subprocess.Popen stdout
[edit] Repeating the call in Intervals and counting the intervals a process were running gives you a good estimation of running time of a process...
[edit2] As GreenAsJade said, you search a way to find out which windows has the focus.
See How do I detect the currently focused application?

Categories

Resources