Detect specific keypresses in GUI
You probably need to handle the "key-press-event" (and probably also "key-release-event") of GtkWidget. PyGtk knows about it.
It should be inside some "windowing" widget, so you may need to use GtkEventBox as a container.
Using Java, How to detect keypresses without using GUI components?
It can be implemented using JNI and/or JNA but this cannot be really called "java implementation" because you will have to write platform specific native code.
Alternative solution I tried is to use full screen transparent widow that is listening to all events of mouse and keyboard and "forwards" them to the real application using class Robot
. I tried this approach. It works well with one limitation: there is a problem to support "mouse over" events of applications: the mouse is not moving over the real application. It is moving over the transparent java window.
Detect key input in Python
You could make a little Tkinter app:
import Tkinter as tk
def onKeyPress(event):
text.insert('end', 'You pressed %s\n' % (event.char, ))
root = tk.Tk()
root.geometry('300x200')
text = tk.Text(root, background='black', foreground='white', font=('Comic Sans MS', 12))
text.pack()
root.bind('<KeyPress>', onKeyPress)
root.mainloop()
Detect a key press in console
If you want to play with the console, you can start with this:
import java.util.Scanner;
public class ScannerTest {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
boolean exit = false;
while (!exit) {
System.out.println("Enter command (quit to exit):");
String input = keyboard.nextLine();
if(input != null) {
System.out.println("Your input is : " + input);
if ("quit".equals(input)) {
System.out.println("Exit programm");
exit = true;
} else if ("x".equals(input)) {
//Do something
}
}
}
keyboard.close();
}
}
Simply run ScannerTest and type any text, followed by 'enter'
Check if a specific key is pressed in tkinter
Bind KeyRelease
or Key
to a function. The function will be called with an argument when the event occurs. The argument will contain all the information about the event.
Sample output:
<KeyPress event state=Mod1|Mod3 keysym=d keycode=68 char='d' x=85 y=111>
now to get the key use event.keysym
sample program:
from tkinter import *
def fun(event):
print(event.keysym, event.keysym=='a')
print(event)
root = Tk()
root.bind("<KeyRelease>", fun)
root.mainloop()
Related Topics
Set Bash Variable from Python Script
Python 3.5 Asyncio and Multiple Websocket Servers
Checking Running Python Script Within the Python Script
Unicodedecodeerror Reading Binary Input
Run Linux Grep Command from Python Subprocess
I Have a Problem with Sending Mail:Typeerror: _Init_() Got an Unexpected Keyword Argument 'Context'
Handling Spreadsheet Data Through the Clipboard in Gtk
Use Df Command to Show Only the %Used
What Is the Correct Way to Include Localisation in Python Packages
Is There Something Wrong with This Python Code, Why Does It Run So Slow Compared to Ruby
Removing Elements That Have Consecutive Duplicates
What Should I Do with "Unexpected Indent" in Python
How to Print the Value of a Tensor Object in Tensorflow