Skip to content

Commit

Permalink
Add key event property (#181)
Browse files Browse the repository at this point in the history
* Add key event property

* check without test

* Fails on firefox

* Remove the commented tests

* Fix adding key property with KeyboarEvent

* Use the correct key value for KeyboardEvent

* set key once
  • Loading branch information
cherifGsoul authored Apr 20, 2020
1 parent cb614a6 commit c9dfa58
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 6 deletions.
4 changes: 3 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
"start": true,
"stop": true,

"formElExp": true
"formElExp": true,
"KeyboardEvent": true

},


Expand Down
20 changes: 16 additions & 4 deletions src/key.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,9 @@ h.extend(syn.key, {

var charCode = keyData[event][0],
keyCode = keyData[event][1],
result = {};
result = {
key: key
};

if (keyCode === 'key') {
result.keyCode = syn.keycodes[key];
Expand All @@ -407,7 +409,7 @@ h.extend(syn.key, {
} else {
result.which = result.charCode;
}

return result;
},
//types of event keys
Expand Down Expand Up @@ -738,8 +740,18 @@ h.extend(syn.create, {
var doc = h.getWindow(element)
.document || document,
event;
if (doc.createEvent) {
try {
if (typeof KeyboardEvent !== 'undefined') {
var keyboardEventKeys = syn.key.keyboardEventKeys;

if (options.key && keyboardEventKeys[options.key]) {
options.key = keyboardEventKeys[options.key];
}

event = new KeyboardEvent(type, options);
event.synthetic = true;
return event;
} else if (doc.createEvent) {
try {
event = doc.createEvent("KeyEvents");
event.initKeyEvent(type, true, true, window, options.ctrlKey, options.altKey, options.shiftKey, options.metaKey, options.keyCode, options.charCode);
} catch (e) {
Expand Down
54 changes: 54 additions & 0 deletions src/keyboard-event-keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
var syn = require('./synthetic');

// For more details please refer to:
// https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values

syn.key.keyboardEventKeys = {
//backspace
'\b': 'Backspace',

//tab
'\t': 'Tab',

//enter
'\r': 'Enter',

//modifier keys
'shift': 'Shift',
'ctrl': 'Control',
'alt': 'Alt',
'meta': 'Meta',

//weird
'pause-break': 'Pause',
'caps': 'CapsLock',
'escape': 'Escape',
'num-lock': 'NumLock',
'scroll-lock': 'ScrollLock',
'print': 'Print',

//navigation
'page-up': 'PageUp',
'page-down': 'PageDown',
'end': 'End',
'home': 'Home',
'left': 'ArrowLeft',
'up': 'ArrowUp',
'right': 'ArrowRight',
'down': 'ArrowDown',
'insert': 'Insert',
'delete': 'Delete',

'f1': 'F1',
'f2': 'F2',
'f3': 'F3',
'f4': 'F4',
'f5': 'F5',
'f6': 'F6',
'f7': 'F7',
'f8': 'F8',
'f9': 'F9',
'f10': 'F10',
'f11': 'F11',
'f12': 'F12'
};
1 change: 1 addition & 0 deletions src/syn.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var syn = require('./synthetic');
require('./keyboard-event-keys');
require('./mouse.support');
require('./browsers');
require('./key.support');
Expand Down
1 change: 0 additions & 1 deletion src/synthetic.js
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,6 @@ extend(syn, {
* @param {Object} autoPrevent
*/
dispatch: function (event, element, type, autoPrevent) {

// dispatchEvent doesn't always work in IE (mostly in a popup)
if (element.dispatchEvent && event) {
var preventDefault = event.preventDefault,
Expand Down
92 changes: 92 additions & 0 deletions test/key_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -721,3 +721,95 @@ QUnit.test("typing in an input type=number works", function() {
start();
});
});


QUnit.test("Key property, a typed", function () {
stop();

var a = false;
st.binder('key', 'keypress', function (ev) {
a = ev.key;
equal('a', ev.key);
});
syn.type('key', "a", function () {
ok(a, "a key typed");
start();
});
});

QUnit.test("Control key", function () {
stop();

var keyIsDown = false;
st.binder("key", "keydown", function (ev) {
keyIsDown = ev.ctrlKey;
ok(ev.key === 'Control', "key is normalized");
});

var keyIsUp = true;
st.binder("key", "keyup", function (ev) {
keyIsUp = ev.ctrlKey;
ok(ev.key === 'Control', "key is normalized");
});

syn.type('key', "[ctrl]", function () {
ok(keyIsDown, "Control modifier key pressed successfully");

syn.type('key', "[ctrl-up]", function () {
ok(!keyIsUp, "Control modifier key released successfully");
start();
});
});
});


QUnit.test("alt keycodes", function () {
stop();

var keyIsDown = false;
st.binder("key", "keydown", function (ev) {
keyIsDown = ev.altKey;
ok(ev.key === 'Alt', "key is normalized");
});

var keyIsUp = true;
st.binder("key", "keyup", function (ev) {
keyIsUp = ev.altKey;
ok(ev.key === 'Alt', "key is normalized");
});

syn.type('key', "[alt]", function () {
ok(keyIsDown, "Alt modifier key pressed successfully");

syn.type('key', "[alt-up]", function () {
ok(!keyIsUp, "Alt modifier key released successfully");
start();
});
});
});

QUnit.test("meta keycodes", function () {
stop();

var keyIsDown = false;
st.binder("key", "keydown", function (ev) {
keyIsDown = ev.metaKey;
ok(ev.key === 'Meta', "key is normalized");
});

var keyIsUp = true;
st.binder("key", "keyup", function (ev) {
keyIsUp = ev.metaKey;
ok(ev.key === 'Meta', "key is normalized");
});

syn.type('key', "[meta]", function () {
ok(keyIsDown, "meta modifier key pressed successfully");

syn.type('key', "[meta-up]", function () {
ok(!keyIsUp, "meta modifier key released successfully");
start();
});
});
});

0 comments on commit c9dfa58

Please sign in to comment.