Skip to content

Commit

Permalink
gui: Implement numlock on Linux
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Schaefer <dhs@frame.work>
  • Loading branch information
JohnAZoidberg committed Jul 13, 2023
1 parent bb680e6 commit 60c4d62
Showing 1 changed file with 30 additions and 11 deletions.
41 changes: 30 additions & 11 deletions qmk_gui.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#!/usr/bin/env python3
import os
import sys
import subprocess
import time

import PySimpleGUI as sg
import hid
# TODO: Linux
from win32api import GetKeyState, keybd_event
from win32con import VK_NUMLOCK, VK_CAPITAL
if os.name == 'nt':
from win32api import GetKeyState, keybd_event
from win32con import VK_NUMLOCK, VK_CAPITAL

import uf2conv

Expand Down Expand Up @@ -118,9 +119,21 @@ def format_fw_ver(fw_ver):
fw_ver_patch = (fw_ver & 0x000F)
return f"{fw_ver_major}.{fw_ver_minor}.{fw_ver_patch}"


def get_numlock_state():
# TODO: Handle Linux
return GetKeyState(VK_NUMLOCK)
if os.name == 'nt':
return GetKeyState(VK_NUMLOCK)
else:
try:
output = subprocess.run(['numlockx', 'status'], stdout=subprocess.PIPE).stdout
if b'on' in output:
return True
elif b'off' in output:
return False
except FileNotFoundError:
# Ignore tool not found, just return None
pass


def main(devices):
device_checkboxes = []
Expand Down Expand Up @@ -187,8 +200,8 @@ def main(devices):
[sg.HorizontalSeparator()],

[sg.Text("OS Numlock Setting")],
[sg.Text("State: "), sg.Text("", k='-NUMLOCK-STATE-'), sg.Push() ,sg.Button("Refresh")],
[sg.Button("Send Numlock Toggle", k='-NUMLOCK-TOGGLE-')],
[sg.Text("State: "), sg.Text("", k='-NUMLOCK-STATE-'), sg.Push() ,sg.Button("Refresh", k='-NUMLOCK-REFRESH-', disabled=True)],
[sg.Button("Send Numlock Toggle", k='-NUMLOCK-TOGGLE-', disabled=True)],

[sg.HorizontalSeparator()],
[sg.Text("Save Settings")],
Expand All @@ -201,7 +214,11 @@ def main(devices):

while True:
numlock_on = get_numlock_state()
if numlock_on is not None:
if numlock_on is None and os != 'nt':
window['-NUMLOCK-STATE-'].update("Unknown, please install the 'numlockx' command")
else:
window['-NUMLOCK-REFRESH-'].update(disabled=False)
window['-NUMLOCK-TOGGLE-'].update(disabled=False)
window['-NUMLOCK-STATE-'].update("On (Numbers)" if numlock_on else "Off (Arrows)")

event, values = window.read()
Expand Down Expand Up @@ -234,9 +251,11 @@ def main(devices):
window['-CHECKBOX-{}-'.format(dev['path'])].update(False, disabled=True)

if event == "-NUMLOCK-TOGGLE-":
# TODO: Linux
keybd_event(VK_NUMLOCK, 0x3A, 0x1, 0)
keybd_event(VK_NUMLOCK, 0x3A, 0x3, 0)
if os.name == 'nt':
keybd_event(VK_NUMLOCK, 0x3A, 0x1, 0)
keybd_event(VK_NUMLOCK, 0x3A, 0x3, 0)
else:
out = subprocess.check_output(['numlockx', 'toggle'])

# Run commands on all selected devices
for dev in selected_devices:
Expand Down

0 comments on commit 60c4d62

Please sign in to comment.