Created
June 27, 2013 15:33
-
-
Save ptone/5877477 to your computer and use it in GitHub Desktop.
recording keypresses with browser & websockets uses: https://github.com/aaugustin/websockets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>key press test</title> | |
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> | |
</head> | |
<body> | |
<h1>Press keys...</h1> | |
WebSocket status : <span id="message"></span> | |
<script> | |
var ws = new WebSocket('ws://127.0.0.1:8866/'); | |
ws.onopen = function(){ | |
$('#message').text('WebSocket open'); | |
}; | |
ws.onmessage = function(ev){ | |
$('#message').text('recieved message'); | |
}; | |
ws.onclose = function(ev){ | |
$('#message').text('WebSocket has closed'); | |
}; | |
ws.onerror = function(ev){ | |
$('#message').text('error occurred'); | |
}; | |
// TODO current bug - won't send up for first key pressed | |
// so just padding the array with a dummy value so slice works | |
var pressedKeys = [999999,]; | |
$(document.body).keydown(function (evt) { | |
if (pressedKeys.indexOf(evt.keyCode) == -1) { | |
pressedKeys.push(evt.keyCode); | |
console.log("down" + evt.keyCode); | |
ws.send(evt.keyCode + ':d'); | |
} | |
}); | |
$(document.body).keyup(function (evt) { | |
if (pressedKeys.indexOf(evt.keyCode)) { | |
var index = pressedKeys.indexOf(evt.keyCode); | |
pressedKeys.splice(index,1); | |
console.log("up" + evt.keyCode); | |
ws.send(evt.keyCode + ':u'); | |
} | |
}); | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tulip | |
import websockets | |
@tulip.coroutine | |
def hello(websocket, uri): | |
while websocket.open: | |
key_info = yield from websocket.recv() | |
print("< {}".format(key_info)) | |
if websocket.open: | |
websocket.send('ack') | |
websockets.serve(hello, 'localhost', 8866) | |
tulip.get_event_loop().run_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment