diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a887478 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +.idea +_pycache_ +pywin32* +*.pyc +tests/ +**/.vscode/.ropeproject/ +**/.vscode/settings.json \ No newline at end of file diff --git a/footpedalkeyboard-driver/.gitignore b/footpedalkeyboard-driver/.gitignore deleted file mode 100644 index d8a9142..0000000 --- a/footpedalkeyboard-driver/.gitignore +++ /dev/null @@ -1,7 +0,0 @@ -.idea -_pycache_ -pywin32* -*.pyc -.vscode/settings.json -tests/ -.vscode/.ropeproject \ No newline at end of file diff --git a/footpedalkeyboard-driver/CHANGELOG.md b/footpedalkeyboard-driver/CHANGELOG.md index 5ec2b0f..abc8007 100644 --- a/footpedalkeyboard-driver/CHANGELOG.md +++ b/footpedalkeyboard-driver/CHANGELOG.md @@ -2,6 +2,13 @@ ## Versions +1.2.0, 2020-11-28 +- Code refactoring +- Code cleanup, with pylint +- Added mapping to VS Code +- Shortened driver python file to driver.py +- Updated conda dependencies + 1.1.1, 2020-11-25 - Fixed typo - Improved documentation diff --git a/footpedalkeyboard-driver/footpedalkeyboard-driver.py b/footpedalkeyboard-driver/driver.py similarity index 73% rename from footpedalkeyboard-driver/footpedalkeyboard-driver.py rename to footpedalkeyboard-driver/driver.py index e502e6a..99c9d57 100644 --- a/footpedalkeyboard-driver/footpedalkeyboard-driver.py +++ b/footpedalkeyboard-driver/driver.py @@ -23,18 +23,17 @@ =============================================================================== """ import sys +import re +import json import serial from serial.tools import list_ports from pyautogui import press, keyDown, keyUp, typewrite -import re -from window_services import get_active_window -import json import psutil import win32process import win32gui -import time +from window_services import get_active_window -DRIVER_VERSION = "1.1.1" +DRIVER_VERSION = "1.2.0" # Teensy USB serial microcontroller program id data: VENDOR_ID = "16C0" @@ -100,6 +99,60 @@ def listen_to_teensy_serial(teensy_port): print() +def match_application_by_window_title(window_title): + """ + Finds a matching application by a given window title + """ + + matching_application = None + + for application_name, application in applications.items(): + # Only process applications that have a regular expression + if not 'window_title_compiled_regex' in application: + continue + + window_title_compiled_regex = application['window_title_compiled_regex'] + if window_title_compiled_regex.match(window_title): + print("Found a matching application by window title =>", + application_name) + matching_application = application + + # Add to cache for next time + if title_application_mapping_cache.__len__() > MAX_CACHE_SIZE: + title_application_mapping_cache.clear() + title_application_mapping_cache[window_title] = application['application_name'] + break + + return matching_application + + +def match_application_by_process_name(window_process_name): + """ + Finds a matching application by a given window process name + """ + + matching_application = None + + for application_name, application in applications.items(): + # Only process applications that have a regular expression + if not 'process_name_compiled_regex' in application: + continue + + process_name_compiled_regex = application['process_name_compiled_regex'] + if process_name_compiled_regex.match(window_process_name): + print("Found a matching application by process name =>", + application_name) + matching_application = application + + # Add to cache for next time + if process_application_mapping_cache.__len__() > MAX_CACHE_SIZE: + process_application_mapping_cache.clear() + process_application_mapping_cache[window_process_name] = application['application_name'] + break + + return matching_application + + def process_footpedalkeyboard_event(event): """ Processes an event coming from the foot pedal keyboard @@ -107,10 +160,10 @@ def process_footpedalkeyboard_event(event): print("RECEIVED KEYBOARD EVENT: " + event) - matchingEvent = event_regex.match(event) - if matchingEvent: - pedal = matchingEvent.group(1) - pedal_action = matchingEvent.group(2) + matching_event = event_regex.match(event) + if matching_event: + pedal = matching_event.group(1) + pedal_action = matching_event.group(2) active_window_title = get_active_window() print("Active window:", active_window_title) @@ -140,37 +193,13 @@ def process_footpedalkeyboard_event(event): # Match application by active window's title if not matching_application: - for application_name, application in applications.items(): - if not 'window_title_compiled_regex' in application: - continue - - window_title_compiled_regex = application['window_title_compiled_regex'] - if window_title_compiled_regex.match(active_window_title): - print("Found a matching application by window title =>", - application_name) - matching_application = application - # Add to cache for next time - if title_application_mapping_cache.__len__() > MAX_CACHE_SIZE: - title_application_mapping_cache.clear() - title_application_mapping_cache[active_window_title] = application['application_name'] - break + matching_application = match_application_by_window_title( + active_window_title) # Match application by active window's process name if not matching_application: - for application_name, application in applications.items(): - if not 'process_name_compiled_regex' in application: - continue - - process_name_compiled_regex = application['process_name_compiled_regex'] - if process_name_compiled_regex.match(active_window_process_name): - print("Found a matching application by process name =>", - application_name) - matching_application = application - # Add to cache for next time - if process_application_mapping_cache.__len__() > MAX_CACHE_SIZE: - process_application_mapping_cache.clear() - process_application_mapping_cache[active_window_process_name] = application['application_name'] - break + matching_application = match_application_by_process_name( + active_window_process_name) if matching_application: apply_application_keys(matching_application, pedal, pedal_action) diff --git a/footpedalkeyboard-driver/key-mappings.json b/footpedalkeyboard-driver/key-mappings.json index 7817f02..d1daa9f 100644 --- a/footpedalkeyboard-driver/key-mappings.json +++ b/footpedalkeyboard-driver/key-mappings.json @@ -101,7 +101,6 @@ { "application_name": "Spotify", "process_name_regex": "Spotify.exe", - "mappings": { "pedalA": { "released": { @@ -239,7 +238,7 @@ "ctrl", "alt", "right" - ] + ] } }, "pedalE": { @@ -252,6 +251,56 @@ } } } + }, + { + "application_name": "Visual Studio Code", + "process_name_regex": "Code.exe", + "mappings": { + "pedalA": { + "released": { + "type": "hotkey", + "keys": [ + "ctrl", + "shift", + "`" + ] + } + }, + "pedalB": { + "released": { + "type": "hotkey", + "keys": [ + "shift", + "left" + ] + } + }, + "pedalC": { + "released": { + "type": "press", + "keys": [ + "F5" + ] + } + }, + "pedalD": { + "released": { + "type": "hotkey", + "keys": [ + "f11" + ] + } + }, + "pedalE": { + "released": { + "type": "hotkey", + "keys": [ + "ctrl", + "right" + ] + } + } + } } ] } \ No newline at end of file diff --git a/footpedalkeyboard-driver/lint.bat b/footpedalkeyboard-driver/lint.bat new file mode 100644 index 0000000..abc8c98 --- /dev/null +++ b/footpedalkeyboard-driver/lint.bat @@ -0,0 +1 @@ +pylint driver.py --extension-pkg-whitelist=win32gui,win32process \ No newline at end of file diff --git a/footpedalkeyboard-driver/py37_fpk.yml b/footpedalkeyboard-driver/py37_fpk.yml index 75a7881..0c4d95d 100644 --- a/footpedalkeyboard-driver/py37_fpk.yml +++ b/footpedalkeyboard-driver/py37_fpk.yml @@ -5,28 +5,29 @@ dependencies: - astroid=2.4.2=py37_0 - autopep8=1.5.4=py_0 - ca-certificates=2020.10.14=0 - - certifi=2020.6.20=pyhd3eb1b0_3 + - certifi=2020.11.8=py37haa95532_0 - colorama=0.4.4=py_0 - isort=5.6.4=py_0 - lazy-object-proxy=1.4.3=py37he774522_0 - mccabe=0.6.1=py37_1 - openssl=1.1.1h=he774522_0 - - pip=19.3.1=py37_0 + - pip=20.2.4=py37haa95532_0 - psutil=5.7.2=py37he774522_0 - pycodestyle=2.6.0=py_0 - pylint=2.6.0=py37_0 - - python=3.7.5=h8c8aaf0_0 + - python=3.7.9=h60c2a47_0 - rope=0.18.0=py_0 - - setuptools=41.6.0=py37_0 - - six=1.15.0=py_0 - - sqlite=3.30.1=he774522_0 + - setuptools=50.3.1=py37haa95532_1 + - six=1.15.0=py37haa95532_0 + - sqlite=3.33.0=h2a8f88b_0 - toml=0.10.1=py_0 - typed-ast=1.4.1=py37he774522_0 - vc=14.1=h0510ff6_4 - - vs2015_runtime=14.16.27012=hf0eaf9b_0 - - wheel=0.33.6=py37_0 + - vs2015_runtime=14.16.27012=hf0eaf9b_3 + - wheel=0.35.1=pyhd3eb1b0_0 - wincertstore=0.2=py37_0 - wrapt=1.11.2=py37he774522_0 + - zlib=1.2.11=h62dcd97_4 - pip: - mouseinfo==0.1.2 - pillow==6.2.1 diff --git a/footpedalkeyboard-driver/run.bat b/footpedalkeyboard-driver/run.bat index 4e2d8b7..ceb63ec 100644 --- a/footpedalkeyboard-driver/run.bat +++ b/footpedalkeyboard-driver/run.bat @@ -6,4 +6,4 @@ ECHO Activating Conda environment: py37_fpk call activate py37_fpk ECHO Executing the FPK Python Driver -python footpedalkeyboard-driver.py \ No newline at end of file +python driver.py \ No newline at end of file diff --git a/footpedalkeyboard-driver/run.sh b/footpedalkeyboard-driver/run.sh index 032d29c..707cf22 100644 --- a/footpedalkeyboard-driver/run.sh +++ b/footpedalkeyboard-driver/run.sh @@ -1,4 +1,4 @@ #!/bin/bash source activate py37_fpk -python footpedalkeyboard-driver.py \ No newline at end of file +python driver.py \ No newline at end of file