Skip to content

Commit 7dd4a07

Browse files
Merge pull request #2493 from arian/fix-keypress-key
Fix event.key for keypress.
2 parents 3ad6cd0 + 8c3699d commit 7dd4a07

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

Source/Types/DOMEvent.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ var DOMEvent = this.DOMEvent = new Type('DOMEvent', function(event, win){
4545

4646
if (type.indexOf('key') == 0){
4747
var code = this.code = (event.which || event.keyCode);
48-
this.key = _keys[code]/*<1.3compat>*/ || Object.keyOf(Event.Keys, code)/*</1.3compat>*/;
48+
this.key = !this.shift && (_keys[code]/*<1.3compat>*/ || Object.keyOf(Event.Keys, code)/*</1.3compat>*/);
4949
if (type == 'keydown' || type == 'keyup'){
5050
if (code > 111 && code < 124) this.key = 'f' + (code - 111);
5151
else if (code > 95 && code < 106) this.key = code - 96;

Specs/Element/Element.Event.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,68 @@ describe('Element.Event keyup with f<key>', function(){
362362

363363
});
364364

365+
describe('Keypress key code', function(){
366+
367+
/*<ltIE8>*/
368+
// return early for IE8- because Syn.js does not fire events
369+
if (!document.addEventListener) return;
370+
/*</ltIE8>*/
371+
372+
var input, key, shift, done;
373+
374+
function keyHandler(e){
375+
key = e.key;
376+
shift = !!e.event.shiftKey;
377+
}
378+
379+
function typeWriter(action){
380+
setTimeout(function () {
381+
Syn.type(action, 'keyTester');
382+
}, 1);
383+
if (done) return true;
384+
}
385+
386+
beforeEach(function(){
387+
input = new Element('input', {
388+
'type': 'text',
389+
'id': 'keyTester'
390+
}).addEvent('keypress', keyHandler).inject(document.body);
391+
});
392+
393+
afterEach(function(){
394+
input.removeEvent('keypress', keyHandler).destroy();
395+
input = key = shift = done = null;
396+
});
397+
398+
it('should return "enter" in event.key', function(){
399+
typeWriter('[enter]');
400+
waits(50);
401+
runs(function(){
402+
expect(key).toBe('enter');
403+
expect(shift).not.toBeTruthy();
404+
});
405+
});
406+
407+
it('should return "1" in event.key', function(){
408+
typeWriter('1');
409+
waits(50);
410+
runs(function(){
411+
expect(key).toBe('1');
412+
expect(shift).not.toBeTruthy();
413+
});
414+
});
415+
416+
it('should return false when pressing SHIFT + 1', function(){
417+
typeWriter('[shift]![shift-up]');
418+
waits(50);
419+
runs(function(){
420+
expect(key).toBe(false);
421+
expect(shift).toBeTruthy();
422+
});
423+
});
424+
425+
});
426+
365427
describe('Element.removeEvent', function(){
366428

367429
it('should remove the onunload method', function(){

0 commit comments

Comments
 (0)