Created
March 18, 2025 00:13
-
-
Save whot/328dbcfa5e397d53aa6fd44c147cbf3c to your computer and use it in GitHub Desktop.
uinput example of forcibly shutting down the machine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import sys | |
| import libevdev | |
| from libevdev import InputEvent | |
| import time | |
| def main(args): | |
| dev = libevdev.Device() | |
| dev.name = "test device" | |
| dev.enable(libevdev.EV_KEY.KEY_POWER) | |
| try: | |
| uinput = dev.create_uinput_device() | |
| print("New device at {} ({})".format(uinput.devnode, uinput.syspath)) | |
| # Sleep for a bit so udev, libinput, Xorg, Wayland, ... all have had | |
| # a chance to see the device and initialize it. Otherwise the event | |
| # will be sent by the kernel but nothing is ready to listen to the | |
| # device yet. | |
| time.sleep(1) | |
| events = [InputEvent(libevdev.EV_KEY.KEY_POWER, 1), | |
| InputEvent(libevdev.EV_SYN.SYN_REPORT, 0)] | |
| uinput.send_events(events) | |
| time.sleep(0.012) | |
| events = [InputEvent(libevdev.EV_KEY.KEY_POWER, 0), | |
| InputEvent(libevdev.EV_SYN.SYN_REPORT, 0)] | |
| uinput.send_events(events) | |
| except OSError as e: | |
| print(e) | |
| if __name__ == "__main__": | |
| main(sys.argv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment