Scotty H
Scotty H

Reputation: 6694

Paper.js Event Behavior Differs Between Browsers

Check out this Paper.js sketch, where you can attempt to paste the sample item by using Ctrl+V. This sketch works in Firefox but not Chrome or Opera (that was the extent of my testing). Why is that and how can this sketch be modified so that I can use Ctrl + V to paste the sample text while running the sketch in Chrome?

Note that when you run it, the key event is logged. In Chrome only the Ctrl keyup event is logged. In Firefox both the V keyup and the Ctrl keyup events are logged.

Upvotes: 3

Views: 194

Answers (1)

bmacnaughton
bmacnaughton

Reputation: 5308

The problem seems to be that Chrome does not generate a keypress event when the control key is depressed. The logic in paperjs depends on the keypress event to (ultimately) generate the keyup event.

It's a bit confusing but you can take a look at paper's Key.js starting on the line that begins DomEvent.add(document, {. You can see that handleKey is only called for a non-special key on the keypress event. handleKey is the code that inserts the key code into charCodeMap. So when the keyup event occurs paper doesn't find code in charCodeMap and doesn't call handleKey.

How to get around this?

What you're doing is straightforward, so maybe just using the native DOM event handling will take care of it:

document.onkeyup = function (e) {
    var code = e.which || e.keyCode;
    if (code === 'v' && e.ctrlKey) {
        // do your pasting here
    }
}

You may need to account for other browser variations, but this should get you started. If you're using jQuery you should consider using jQuery's keyup function. That handles cross browser issues transparently.

Upvotes: 2

Related Questions