Skip to content

Commit

Permalink
add key_map
Browse files Browse the repository at this point in the history
  • Loading branch information
nalbam committed Nov 11, 2019
1 parent bd70836 commit 76c13c0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 82 deletions.
73 changes: 24 additions & 49 deletions public/timer.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,14 @@ let timer = new Timer(
let socket = io();
socket.on('timer', function (name) {
console.log(`socket timer ${name}`);
exec(name);
});

function send(name) {
socket.emit('timer', name);
}

function exec(name) {
switch (name) {
case 'start':
timer.start();
Expand All @@ -258,61 +266,28 @@ socket.on('timer', function (name) {
timer.squeeze();
break;
}
});

function call(name) {
socket.emit('timer', name);
}

let key_map = {
'81': 'start',
'87': 'pause',
'69': 'passed',
'82': 'reset',
'84': 'clear',
'89': 'remove',
'71': 'squeeze',
};

document.addEventListener('keydown', function (event) {
switch (event.keyCode) {
case 81: // q
call('start');
break;
case 87: // w
call('pause');
break;
case 69: // e
call('passed');
break;
case 82: // r
call('reset');
break;
case 84: // t
call('clear');
break;
case 89: // y
call('remove');
break;
case 71: // g
call('squeeze');
break;
}
console.log(`keydown ${event.keyCode} : ${key_map[event.keyCode]}`);

send(key_map[event.keyCode]);
});

function btn_listener(event) {
switch (event.target.id) {
case 'btn_start':
// call('start');
timer.start();
break;
case 'btn_pause':
// call('pause');
timer.pause();
break;
case 'btn_passed':
// call('passed');
timer.passed();
break;
case 'btn_reset':
// call('reset');
timer.reset();
break;
case 'btn_clear':
// call('clear');
timer.clear();
break;
}
let name = event.target.id.substring(4);

exec(name);
}

document.getElementById('btn_start').addEventListener('click', btn_listener);
Expand Down
54 changes: 21 additions & 33 deletions public/timer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -234,26 +234,23 @@ ws.onclose = close;
ws.onmessage = message;
ws.onerror = console.log;

// connect to the web socket
function open() {
console.log('WebSocket open');
console.log('ws open');
}

// report a closed web socket connection
function close() {
console.log('WebSocket close');
console.log('ws close');
}

// write a message into main
function message(e) {
let msg = JSON.parse(e.data);

console.log(`WebSocket message : ${msg.text}`);
console.log(`ws message : ${msg.text}`);

exec(msg.text);
}

function call(name) {
function send(name) {
var msg = {
type: 'message',
text: name
Expand Down Expand Up @@ -290,37 +287,28 @@ function exec(name) {
}
}

let key_map = {
'81': 'start',
'87': 'pause',
'69': 'passed',
'82': 'reset',
'84': 'clear',
'89': 'remove',
'71': 'squeeze',
};

document.addEventListener('keydown', function (event) {
console.log(`keydown ${event.keyCode} : ${key_map[event.keyCode]}`);

send(key_map[event.keyCode]);
});

function btn_listener(event) {
let name = event.target.id.substring(4);

exec(name);
}

document.addEventListener('keydown', function (event) {
switch (event.keyCode) {
case 81: // q
call('start');
break;
case 87: // w
call('pause');
break;
case 69: // e
call('passed');
break;
case 82: // r
call('reset');
break;
case 84: // t
call('clear');
break;
case 89: // y
call('remove');
break;
case 71: // g
call('squeeze');
break;
}
});

document.getElementById('btn_start').addEventListener('click', btn_listener);
document.getElementById('btn_pause').addEventListener('click', btn_listener);
document.getElementById('btn_passed').addEventListener('click', btn_listener);
Expand Down

0 comments on commit 76c13c0

Please sign in to comment.