Generating keyboard events

124 views
Skip to first unread message

Chris Strom

unread,
Sep 6, 2013, 1:31:49 PM9/6/13
to General Dart Discussion
Dispatching custom keyboard events with data has always been a little tricky. It was doable, if hacky, with the keyIdentifier named parameter in the KeyboardEvent constructor. This went away in r27025.

The documentation for KeyboardEvent would seem to indicate that it is possible to create custom KeyEvent objects with “specific key value contents,” but I'll be darned if I can figure out how. The constructor only accepts a single KeyboardEvent object, which no longer allows setting character data. All of the properties are get-only.

So am I just out of luck here? 

-Chris

Bob Nystrom

unread,
Sep 11, 2013, 11:49:43 AM9/11/13
to General Dart Discussion, Emily Fortuna
+Emily, our resident keyboard event wizard. :)

- bob


--
For other discussions, see https://groups.google.com/a/dartlang.org/
 
For HOWTO questions, visit http://stackoverflow.com/tags/dart
 
To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.

Emily Fortuna

unread,
Sep 11, 2013, 1:02:49 PM9/11/13
to Bob Nystrom, General Dart Discussion
Hi Chris,
I read your blog post about Keyboard Events a little while back which is what prompted me to make the change. The fact is, with the old constructor setting keyIdentifier didn't really even work because it wasn't cross-browser compatible. It only worked on webkit/blink-based browsers. KeyboardEvent is intended to expose the base level data, and be consistent across platforms. So rather than giving you functionality that isn't reliable, you get... nothing! No, I'm just kidding. I've added the functionality back in a different place.

What I think you'd like is the new functionality going into KeyEvent. I have a CL to add this to KeyEvent that is aaaaalmost ready to go in: https://codereview.chromium.org/23455033/ that will add this functionality. It does some crazy polyfilling (who knew creating your own keyboard events consistently across all browsers could be so hard?) but it will allow you to programmatically create key(board) event. It uses the charCode/keyCode system, which is better for understanding what keys are actually pressed on different international keyboards:

var stream = KeyEvent.keyPressEvent.forTarget(document.body);
var subscription = stream.listen((keyEvent) {
  print(keyEvent.charCode);
  print(keyEvent.keyCode);
});

var k = new KeyEvent('keypress', keyCode: 65, charCode: 97);
stream.add(k);

I apologize for the current lapse in functionality, but this change should be in by the end of the week, and come out in the next version of the SDK!

Chris Strom

unread,
Sep 11, 2013, 1:43:01 PM9/11/13
to General Dart Discussion
Hah! And here I thought I was being paranoid thinking that keyIdentifier went away because I was abusing it :D

But seriously, that CL looks great and the API feels like it's _exactly_ what I'm looking for. I'll be first in line to give it a try when it's ready.

Much thanks!

-Chris


Jos Hirth

unread,
Sep 11, 2013, 7:48:38 PM9/11/13
to mi...@dartlang.org, Bob Nystrom
Heh. That looks like the fix for #11164. :)
Reply all
Reply to author
Forward
0 new messages