onlycode.org

javascript
jquery
html
css
angularjs
node.js
ajax
html5
php
json
arrays
google-chrome
dom
regex
reactjs
ecmascript-6
javascript-events

your's history:
firing a keyboard event...

Firing a Keyboard Event in JavaScript, just only code

keyboard-events | webkit | safari | javascript-events | javascript

edit
function simulateKeyEvent(character) {
  var evt = document.createEvent("KeyboardEvent");
  (evt.initKeyEvent || evt.initKeyboardEvent)("keypress", true, true, window,
                    0, 0, 0, 0,
                    0, character.charCodeAt(0)) 
  var canceled = !body.dispatchEvent(evt);
  if(canceled) {
    // A handler called preventDefault
    alert("canceled");
  } else {
    // None of the handlers called preventDefault
    alert("not canceled");
  }
}
function simulateKeyPress(character) {
  jQuery.event.trigger({ type : 'keypress', which : character.charCodeAt(0) });
}

29
tyronegcarter, 07.05.2011 12:26




edit
element.addEventListener("keydown", function(e){ console.log(e.key, e.char, e.keyCode) })

var e = new KeyboardEvent("keydown", {bubbles : true, cancelable : true, key : "Q", char : "Q", shiftKey : true});
element.dispatchEvent(e);

//If you need legacy property "keyCode"
// Note: In some browsers you can't overwrite "keyCode" property. (At least in Safari)
delete e.keyCode;
Object.defineProperty(e, "keyCode", {"value" : 666})
var e = new KeyboardEvent("keydown", {
    bubbles : true,
    cancelable : true,
    char : "Q",
    key : "q",
    shiftKey : true,
    keyCode : 81
});

28
termi, 09.08.2012 17:44




edit
event.initKeyEvent (type, bubbles, cancelable, viewArg, 
       ctrlKeyArg, altKeyArg, shiftKeyArg, metaKeyArg, 
           keyCodeArg, charCodeArg)

26
ibrahim mahrir, 07.06.2009 11:57

Add your just only code

your name:    



If you need full text click here

Some similar links Firing a Keyboard Event in JavaScript ↓:
Firing a keyboard event on Chrome

How can I give keyboard focus to a DIV and attach keyboard event handlers to it?

Prevent click event from firing when dblclick event fires

which is the best Javascript Keyboard event library.(Hotkeys,Shortcuts ) [closed]



This project is created for those who just need only code, wihout maaaaaaaaaaany words.