Skip to content

Commit

Permalink
Refactor out keystrokeId (#262)
Browse files Browse the repository at this point in the history
Using socket.io's callbacks, we don't really need to track keystroke IDs, so this removes them.
  • Loading branch information
mtlynch authored Sep 22, 2020
1 parent 0c3347d commit cd1c91c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 75 deletions.
10 changes: 4 additions & 6 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,26 +66,24 @@ def socket_keystroke(message):
logger.error('Failed to parse keystroke request: %s', e)
return
hid_keycode = None
processing_result = {'keystrokeId': keystroke.id, 'success': False}
try:
control_keys, hid_keycode = js_to_hid.convert(keystroke,
keyboard_layout)
except js_to_hid.UnrecognizedKeyCodeError:
logger.warning('Unrecognized key: %s (keycode=%d)', keystroke.key,
keystroke.key_code)
return processing_result
return {'success': False}
if hid_keycode is None:
logger.info('Ignoring %s key (keycode=%d)', keystroke.key,
keystroke.key_code)
return processing_result
return {'success': False}
try:
fake_keyboard.send_keystroke(keyboard_path, control_keys, hid_keycode)
except hid_write.WriteError as e:
logger.error('Failed to write key: %s (keycode=%d). %s', keystroke.key,
keystroke.key_code, e)
return processing_result
processing_result['success'] = True
return processing_result
return {'success': False}
return {'success': True}


@socketio.on('mouse-event')
Expand Down
3 changes: 0 additions & 3 deletions app/request_parsers/keystroke.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class InvalidKeyCode(Error):

@dataclasses.dataclass
class Keystroke:
id: int
left_ctrl_modifier: bool
left_shift_modifier: bool
left_alt_modifier: bool
Expand All @@ -34,7 +33,6 @@ def parse_keystroke(message):
raise MissingField(
'Keystroke parameter is invalid, expecting a dictionary data type')
required_fields = (
'id',
'key',
'keyCode',
'ctrlKey',
Expand All @@ -48,7 +46,6 @@ def parse_keystroke(message):
raise MissingField(
'Keystroke request is missing required field: %s' % field)
return Keystroke(
id=message['id'],
left_ctrl_modifier=_parse_modifier_key(message['ctrlKey']),
left_shift_modifier=_parse_modifier_key(message['shiftKey']),
left_alt_modifier=_parse_modifier_key(message['altKey']),
Expand Down
42 changes: 10 additions & 32 deletions app/static/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const socket = io();
let connectedToServer = false;
let keystrokeId = 0;

const screenCursorOptions = [
"disabled", //to show on disconnect
Expand Down Expand Up @@ -41,9 +40,9 @@ function limitRecentKeys(limit) {
}
}

function addKeyCard(key, keystrokeId) {
function addKeyCard(key) {
if (!shouldDisplayKeyHistory()) {
return;
return null;
}
const card = document.createElement("div");
card.classList.add("key-card");
Expand All @@ -53,28 +52,9 @@ function addKeyCard(key, keystrokeId) {
}
card.style.fontSize = `${1.1 - 0.08 * keyLabel.length}em`;
card.innerText = keyLabel;
card.setAttribute("keystroke-id", keystrokeId);
document.getElementById("recent-keys").appendChild(card);
limitRecentKeys(10);
}

function updateKeyStatus(keystrokeId, success) {
if (!shouldDisplayKeyHistory()) {
return;
}
const recentKeysDiv = document.getElementById("recent-keys");
const cards = recentKeysDiv.children;
for (let i = 0; i < cards.length; i++) {
const card = cards[i];
if (parseInt(card.getAttribute("keystroke-id")) === keystrokeId) {
if (success) {
card.classList.add("processed-key-card");
} else {
card.classList.add("unsupported-key-card");
}
return;
}
}
return card;
}

function showError(errorType, errorMessage) {
Expand Down Expand Up @@ -138,16 +118,17 @@ function browserLanguage() {

// Send a keystroke message to the backend, and add a key card to the web UI.
function sendKeystroke(keystroke) {
let keyCard = undefined;
if (!keystroke.metaKey) {
addKeyCard(keystroke.key, keystroke.id);
keyCard = addKeyCard(keystroke.key);
}
socket.emit("keystroke", keystroke, (result) => {
updateKeyStatus(result.keystrokeId, result.success);
if (keyCard && result.success) {
keyCard.classList.add("processed-key-card");
} else {
keyCard.classList.add("unsupported-key-card");
}
});
if (!keystroke.metaKey) {
// Increment the global keystroke ID.
keystrokeId++;
}
}

function onSocketConnect() {
Expand Down Expand Up @@ -192,7 +173,6 @@ function onKeyDown(evt) {
}

sendKeystroke({
id: keystrokeId,
metaKey: evt.metaKey || document.getElementById("meta-modifier").pressed,
altKey: evt.altKey || document.getElementById("alt-modifier").pressed,
shiftKey: evt.shiftKey || document.getElementById("shift-modifier").pressed,
Expand Down Expand Up @@ -258,7 +238,6 @@ function sendTextInput(textInput) {
// the lowercase key.
const requiresShiftKey = /^[A-Z¬!"£$%^&\*()_\+{}|<>\?:@~#]/;
sendKeystroke({
id: keystrokeId,
metaKey: false,
altKey: false,
shiftKey: requiresShiftKey.test(textInput[i]),
Expand All @@ -267,7 +246,6 @@ function sendTextInput(textInput) {
sysrqKey: false,
key: key,
keyCode: keyCode,
keystrokeId: keystrokeId,
location: null,
});
}
Expand Down
36 changes: 2 additions & 34 deletions app/tests/request_parsers/test_keystroke.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@ class KeystrokeTest(unittest.TestCase):

def test_parses_valid_keystroke_message(self):
self.assertEqual(
keystroke.Keystroke(id=123,
left_meta_modifier=False,
keystroke.Keystroke(left_meta_modifier=False,
left_alt_modifier=False,
left_shift_modifier=False,
left_ctrl_modifier=False,
right_alt_modifier=False,
key='A',
key_code=65),
keystroke.parse_keystroke({
'id': 123,
'metaKey': False,
'altKey': False,
'shiftKey': False,
Expand All @@ -38,16 +36,14 @@ def test_parses_valid_keystroke_message(self):

def test_parses_valid_keystroke_message_with_all_modifiers_pushed(self):
self.assertEqual(
keystroke.Keystroke(id=456,
left_meta_modifier=True,
keystroke.Keystroke(left_meta_modifier=True,
left_alt_modifier=True,
left_shift_modifier=True,
left_ctrl_modifier=True,
right_alt_modifier=True,
key='A',
key_code=65),
keystroke.parse_keystroke({
'id': 456,
'metaKey': True,
'altKey': True,
'shiftKey': True,
Expand All @@ -60,7 +56,6 @@ def test_parses_valid_keystroke_message_with_all_modifiers_pushed(self):
def test_rejects_invalid_meta_modifier(self):
with self.assertRaises(keystroke.InvalidModifierKey):
keystroke.parse_keystroke({
'id': 123,
'metaKey': 'banana',
'altKey': False,
'shiftKey': False,
Expand All @@ -73,7 +68,6 @@ def test_rejects_invalid_meta_modifier(self):
def test_rejects_invalid_alt_modifier(self):
with self.assertRaises(keystroke.InvalidModifierKey):
keystroke.parse_keystroke({
'id': 123,
'metaKey': False,
'altKey': 'banana',
'shiftKey': False,
Expand All @@ -86,7 +80,6 @@ def test_rejects_invalid_alt_modifier(self):
def test_rejects_invalid_shift_modifier(self):
with self.assertRaises(keystroke.InvalidModifierKey):
keystroke.parse_keystroke({
'id': 123,
'metaKey': False,
'altKey': False,
'shiftKey': 'banana',
Expand All @@ -99,7 +92,6 @@ def test_rejects_invalid_shift_modifier(self):
def test_rejects_invalid_ctrl_modifier(self):
with self.assertRaises(keystroke.InvalidModifierKey):
keystroke.parse_keystroke({
'id': 123,
'metaKey': False,
'altKey': False,
'shiftKey': False,
Expand All @@ -112,7 +104,6 @@ def test_rejects_invalid_ctrl_modifier(self):
def test_rejects_invalid_alt_graph_modifier(self):
with self.assertRaises(keystroke.InvalidModifierKey):
keystroke.parse_keystroke({
'id': 123,
'metaKey': False,
'altKey': False,
'shiftKey': False,
Expand All @@ -125,7 +116,6 @@ def test_rejects_invalid_alt_graph_modifier(self):
def test_rejects_negative_keycode_value(self):
with self.assertRaises(keystroke.InvalidKeyCode):
keystroke.parse_keystroke({
'id': 123,
'metaKey': False,
'altKey': False,
'shiftKey': False,
Expand All @@ -138,7 +128,6 @@ def test_rejects_negative_keycode_value(self):
def test_rejects_too_high_keycode_value(self):
with self.assertRaises(keystroke.InvalidKeyCode):
keystroke.parse_keystroke({
'id': 123,
'metaKey': False,
'altKey': False,
'shiftKey': False,
Expand All @@ -151,7 +140,6 @@ def test_rejects_too_high_keycode_value(self):
def test_rejects_string_keycode_value(self):
with self.assertRaises(keystroke.InvalidKeyCode):
keystroke.parse_keystroke({
'id': 123,
'metaKey': False,
'altKey': False,
'shiftKey': False,
Expand All @@ -164,7 +152,6 @@ def test_rejects_string_keycode_value(self):
def test_rejects_float_keycode_value(self):
with self.assertRaises(keystroke.InvalidKeyCode):
keystroke.parse_keystroke({
'id': 123,
'metaKey': False,
'altKey': False,
'shiftKey': False,
Expand All @@ -174,22 +161,9 @@ def test_rejects_float_keycode_value(self):
'keyCode': 1.25,
})

def test_rejects_missing_id_value(self):
with self.assertRaises(keystroke.MissingField):
keystroke.parse_keystroke({
'metaKey': False,
'altKey': False,
'shiftKey': False,
'ctrlKey': False,
'altGraphKey': False,
'key': 'A',
'keyCode': 1,
})

def test_rejects_missing_meta_key_value(self):
with self.assertRaises(keystroke.MissingField):
keystroke.parse_keystroke({
'id': 123,
'altKey': False,
'shiftKey': False,
'ctrlKey': False,
Expand All @@ -201,7 +175,6 @@ def test_rejects_missing_meta_key_value(self):
def test_rejects_missing_alt_key_value(self):
with self.assertRaises(keystroke.MissingField):
keystroke.parse_keystroke({
'id': 123,
'metaKey': False,
'shiftKey': False,
'ctrlKey': False,
Expand All @@ -213,7 +186,6 @@ def test_rejects_missing_alt_key_value(self):
def test_rejects_missing_alt_graph_key_value(self):
with self.assertRaises(keystroke.MissingField):
keystroke.parse_keystroke({
'id': 123,
'altKey': False,
'metaKey': False,
'shiftKey': False,
Expand All @@ -225,7 +197,6 @@ def test_rejects_missing_alt_graph_key_value(self):
def test_rejects_missing_shift_key_value(self):
with self.assertRaises(keystroke.MissingField):
keystroke.parse_keystroke({
'id': 123,
'metaKey': False,
'altKey': False,
'ctrlKey': False,
Expand All @@ -237,7 +208,6 @@ def test_rejects_missing_shift_key_value(self):
def test_rejects_missing_ctrl_key_value(self):
with self.assertRaises(keystroke.MissingField):
keystroke.parse_keystroke({
'id': 123,
'metaKey': False,
'altKey': False,
'shiftKey': False,
Expand All @@ -249,7 +219,6 @@ def test_rejects_missing_ctrl_key_value(self):
def test_rejects_missing_key_value(self):
with self.assertRaises(keystroke.MissingField):
keystroke.parse_keystroke({
'id': 123,
'metaKey': False,
'altKey': False,
'shiftKey': False,
Expand All @@ -261,7 +230,6 @@ def test_rejects_missing_key_value(self):
def test_rejects_missing_key_code_value(self):
with self.assertRaises(keystroke.MissingField):
keystroke.parse_keystroke({
'id': 123,
'metaKey': False,
'altKey': False,
'shiftKey': False,
Expand Down

0 comments on commit cd1c91c

Please sign in to comment.