How can I use python xlib to generate a single keypress?

Dolores

I want to make a very simple python 3 script that will generate a single keypress (F15). I don't want to use a bunch of libraries to do this as I only need one key to be pressed and don't need support for the whole keyboard. I know I need to use KeyPress and KeyRelease in order to generate a keyboard event. I'm just not sure where exactly to start and the documentation is a little confusing.

http://tronche.com/gui/x/xlib/events/keyboard-pointer/keyboard-pointer.html http://python-xlib.sourceforge.net/?page=documentation

tynn

I'll use ctypes to show you how it could work, but porting it to python-xlib should be straightforward. So lets start with loading the library:

import ctypes
X11 = ctypes.CDLL("libX11.so")

and defining the structures needed:

class Display(ctypes.Structure):
    """ opaque struct """

class XKeyEvent(ctypes.Structure):
    _fields_ = [
            ('type', ctypes.c_int),
            ('serial', ctypes.c_ulong),
            ('send_event', ctypes.c_int),
            ('display', ctypes.POINTER(Display)),
            ('window', ctypes.c_ulong),
            ('root', ctypes.c_ulong),
            ('subwindow', ctypes.c_ulong),
            ('time', ctypes.c_ulong),
            ('x', ctypes.c_int),
            ('y', ctypes.c_int),
            ('x_root', ctypes.c_int),
            ('y_root', ctypes.c_int),
            ('state', ctypes.c_uint),
            ('keycode', ctypes.c_uint),
            ('same_screen', ctypes.c_int),
        ]

class XEvent(ctypes.Union):
    _fields_ = [
            ('type', ctypes.c_int),
            ('xkey', XKeyEvent),
            ('pad', ctypes.c_long*24),
        ]

X11.XOpenDisplay.restype = ctypes.POINTER(Display)

Now we just need to send the event to the root window:

display = X11.XOpenDisplay(None)
key = XEvent(type=2).xkey #KeyPress
key.keycode = X11.XKeysymToKeycode(display, 0xffcc) #F15
key.window = key.root = X11.XDefaultRootWindow(display)
X11.XSendEvent(display, key.window, True, 1, ctypes.byref(key))
X11.XCloseDisplay(display)

This minimal example worked well for me (just using F2 instead). The same can be done to send a KeyRelease event. If a special window is to be targeted, key.window should be set appropriately.

I'm not sure, if it's necessary to use the XEvent union, since it'd worked with the XKeyEvent all alone for me, but it's better to be safe.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I use keypress and prompt libraries same time?

How can I use a variable to generate barcode with python-barcode

How do I record a single keypress in Scheme?

how can I generate a single QR code for multiple links by using python?

How can I simulate a keypress in JavaScript?

How can i make a keypress action to this code?

how can i mute an iframe on keypress

How can I generate a tree in Python?

How can I generate a dictionary of lists in python?

What are the Xlib header files and how can I install them?

How can I use setuptools to generate a console_scripts entry point which calls `python -m mypackage`?

How can I use Python to generate nested JSON data from my CSV file

How can I generate this?

How can I use python cv2.SimpleBlobDetector to detect the single largest spot in an image?

How can I reuse logic to handle a keypress and a button click in Python's tkinter GUI?

Python: Generate a date time string that I can use in for MySQL

How to generate keypress event programmatically

How can I use ASM to generate invokedynamic calls that simulate invokevirtual

How can I use an "if" to generate part of a shell pipeline?

How can I use RxJS to generate a requestAnimationFrame loop?

How can I use sed to generate an awk file?

How can I dynamically generate a list of filenames for use in a task with Grunt?

How can I use a for loop to programmatically generate snakemake rules?

How can I generate a random value and then use the pop method to remove it?

Please how can i use generate a formula in pandas

How to use $(this) on keypress

How can I use single file vue component with symfony 4?

How can I use regular and bold in a single String?

Pandas: How can I use the apply() function for a single column?