Can I simulate keyboard across browsers using websockets. I am looking to simulate Arrow Keys and F11.
Asked
Active
Viewed 2,303 times
1
-
2To do what? What have you tried? – J. Steen Aug 12 '12 at 09:57
-
I am having a iframe inside my webpage. This iframe element, Google presentation can be controlled using the keyboards. But since I want to send the events from a remote browser, I want to simulate the key events. Thanks – user279244 Aug 18 '12 at 17:05
2 Answers
4
Key Events have been available in browsers for many years
See http://www.quirksmode.org/dom/events/keys.html for a list of support
And working with key events in jQuery here Is it possible to simulate key press events programmatically?
Websockets is newer technology which is not available in all browsers but you can use libraries which simulate web sockets if needed on older browsers like Socket.io to do bidirectional real-time messaging.
The pseudo code on the client would look something like this:
(function ($) {
function simulateKeyPress(character) {
jQuery.event.trigger({ type : 'keypress', which : character.charCodeAt(0) });
}
function onkeypress(key, ts) {
var act = {"timestamp" : ts, "key" : key};
socketIoClient.emit("message", act);
}
$('body').keypress(function(e) {
// Send date in case you want to order/buffer on the server
onkeypress(e.which, new Date());
});
var socketIoClient = io.connect(null, {
'port': '#socketIoPort#'
, 'rememberTransport': true
, 'transports': ['websocket', 'xhr-polling'] // put here all the transports you need
});
// Handle the key press events
socketIoClient.on('message', function(json) {
var act = JSON.parse(json);
if (act) {
// Not sure what the keys will do but if you just want to echo them
simulateKeyPress(act.key);
}
});
})(jQuery);
Hope this helps

Community
- 1
- 1

Monica Wilkinson
- 635
- 1
- 8
- 11
0
of course you can!
- open a connection to Websocket server and send request to it on every jQuery keyUp-Event!
- On the websocket side, just pass the Event throw to your other Browser connection.
- On the other Browser just simulate the KeyPress
simulateKeyPress(e);

tn4foxxah
- 287
- 2
- 11