Permalink
Browse files

Implemented rudimentary charCode to be returned by keydown event.

  • Loading branch information...
1 parent bbe24b4 commit a36aab80f4f1b5294eb5d3045b2b7335eef5a0e4 Wolfram Kriesing committed Feb 12, 2013
Showing with 75 additions and 0 deletions.
  1. +5 −0 CHANGELOG
  2. +8 −0 src/renderer/svg/svg_event_handlers.js
  3. +62 −0 test/renderer/svg-spec.js
View
@@ -1,3 +1,8 @@
+v0.4.4
+-------------------
+* Added a basic implementation of charCode to return for
+ keydown events, as long as they are broken in DOM
+
v0.4.3
-------------------
* Add support for `strokeDash` and `strokeDashOffset` attributes on all Paths.
@@ -236,6 +236,14 @@ define([
// There is another currently focused element (outside of the stage), exit:
return;
}
+ var charCode = domEvent.charCode;
+ if (!charCode) {
+ charCode = domEvent.keyCode;
+ }
+ if (domEvent.shiftKey) {
+ charCode = (String.fromCharCode(charCode)).toUpperCase().charCodeAt(0);
@davidaurelio
davidaurelio Mar 1, 2013 Member

I’ve checked this in different browsers (chrome, safari, firefox, opera, ie10), and it’s not necessary. charCode already contains the correct code. On the contrary, for combinations like shift–alt–5 on a mac/us layout, charCode would be 64257 for “fi” (one character!), toUpperCase() would transform that to “FI”, and eventually charCodeAt() would return 70 for “F”.

Edit: Also tested IE9 and safari/ios. toUpperCase is not necessary.

@davidaurelio
davidaurelio Mar 1, 2013 Member

Another problem is stuff like alt+u, a on a mac/us keyboard, which produces an “¨”-accent and an “a” and combines them to “ä”. That combination does not trigger a single keypress, though.

+ }
+ event.charCode = charCode;
event.keyCode = domEvent.keyCode;
event.ctrlKey = domEvent.ctrlKey;
event.altKey = domEvent.altKey;
@@ -231,6 +231,68 @@ define(['bonsai/renderer/svg/svg'], function(SvgRenderer) {
.toBe(1);
renderer.removeListener('userevent', listener);
});
+
+ describe('keydown events', function() {
+ function createDomKeyDownEvent(keyCode, charCode, shiftKey) {
+ // found working solution at http://stackoverflow.com/questions/8942678/keyboardevent-in-chrome-keycode-is-0/12522752#12522752
+ var evt = document.createEvent("Events");
+ evt.initEvent("keydown", true, true);
+ evt.altKey = false;
+ evt.ctrlKey = false;
+ evt.shiftKey = !!shiftKey;
+ evt.metaKey = false;
+ evt.keyCode = keyCode;
+ evt.charCode = charCode;
+ return evt;
+ }
+ var evtData, renderer, listener;
+ function fireKeyDown(keyCode, charCode, shiftKey) {
+ evtData = null;
+ listener = function(e) { if (e.type=='keydown') evtData = e; };
+ renderer = createSvgRenderer();
+ renderer.on('userevent', listener);
+ document.dispatchEvent(createDomKeyDownEvent(keyCode, charCode, shiftKey));
+ }
+ afterEach(function() {
+ renderer.removeListener('userevent', listener);
+ });
+ it('should pass the keyCode for a key press', function() {
+ var keyCode = 65;
+ fireKeyDown(keyCode);
+ expect(evtData.keyCode)
+ .toBe(keyCode);
+ });
+ describe('charCode delivered by the event', function() {
+ it('should pass the charCode for a key press', function() {
+ var charCode = 'a'.charCodeAt(0);
+ fireKeyDown(65, charCode);
+ expect(evtData.charCode)
+ .toBe(charCode);
+ });
+ it('should pass the charCode "A" properly', function() {
+ var charCode = 'A'.charCodeAt(0);
+ fireKeyDown(65, charCode);
+ expect(evtData.charCode)
+ .toBe(charCode);
+ });
+ });
+ describe('charCode not given by the event', function() {
+ it('should create the correct charCode for "a" (lower case)', function() {
+ var charCode = 'a';
+ var keyCode = charCode.charCodeAt(0);
+ fireKeyDown(keyCode);
+ expect(evtData.charCode)
+ .toBe(keyCode);
+ });
+ it('should create the correct charCode for "A" (upper case)', function() {
+ var charCode = 'A';
+ var keyCode = charCode.toLowerCase().charCodeAt(0);
+ fireKeyDown(keyCode, null, true);
+ expect(evtData.charCode)
+ .toBe(charCode.charCodeAt(0));
+ });
+ });
+ });
});
});
});

0 comments on commit a36aab8

Please sign in to comment.