Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/lint refactor #5

Merged
merged 4 commits into from
Nov 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.idea
_pycache_
pywin32*
*.pyc
tests/
**/.vscode/.ropeproject/
**/.vscode/settings.json
7 changes: 0 additions & 7 deletions footpedalkeyboard-driver/.gitignore

This file was deleted.

7 changes: 7 additions & 0 deletions footpedalkeyboard-driver/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -100,17 +99,71 @@ 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
"""

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)
Expand Down Expand Up @@ -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)
Expand Down
53 changes: 51 additions & 2 deletions footpedalkeyboard-driver/key-mappings.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@
{
"application_name": "Spotify",
"process_name_regex": "Spotify.exe",

"mappings": {
"pedalA": {
"released": {
Expand Down Expand Up @@ -239,7 +238,7 @@
"ctrl",
"alt",
"right"
]
]
}
},
"pedalE": {
Expand All @@ -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"
]
}
}
}
}
]
}
1 change: 1 addition & 0 deletions footpedalkeyboard-driver/lint.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pylint driver.py --extension-pkg-whitelist=win32gui,win32process
17 changes: 9 additions & 8 deletions footpedalkeyboard-driver/py37_fpk.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion footpedalkeyboard-driver/run.bat
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ ECHO Activating Conda environment: py37_fpk
call activate py37_fpk

ECHO Executing the FPK Python Driver
python footpedalkeyboard-driver.py
python driver.py
2 changes: 1 addition & 1 deletion footpedalkeyboard-driver/run.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash

source activate py37_fpk
python footpedalkeyboard-driver.py
python driver.py