简体   繁体   中英

Simulate key press in browser?

Is it possible to simulate key press on the browser (js, canvas, server side?), let says I have some key presses in some data structures, left right up and down there is a flash file loaded so instead of me typing left right up and down the browser execute it and the flash reads those things? Kind of like a macro.

I'm not exactly sure if it would work on a Flash embed but you can definitely simulate keypresses by using JavaScript.

If you haven't already, you should check out jQuery. (If you want to Get Things Done, fast with JavaScript, and are new, then I would suggest to don't even bother learning JavaScript and learn jQuery instead (jQuery is a JavaScript Library, so you'd still be using/writing javascript, except with a much more powerful set of toys))

in jQuery to do a Left,Right,Up,Down would be...

<script>
$(function(){
  var left = $.Event('keypress');
  left.which = 37;
  var right = $.Event('keypress');
  right.which = 39;
  var up = $.Event('keypress');
  up.which = 38;
  var down = $.Event('keypress');
  down.which = 40;

  $('embed')
    .trigger(left)
    .trigger(right)
    .trigger(up)
    .trigger(down)
  ;
});
</script>

This might not give you exactly what you are looking for, but have a look at jQuery sendkeys extension http://bililite.com/blog/2011/01/23/improved-sendkeys/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM