3

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Scotty H
  • 6,432
  • 6
  • 41
  • 94
  • http://stackoverflow.com/questions/4954403/can-jquery-keypress-detect-more-than-one-key-at-the-same-time – Eleanor Zimmermann Dec 16 '15 at 01:29
  • ^ That looks like it could possibly be relevant. Possibly you need to also consider keydown to keep track of two keys going on at the same time. In Chrome, I can get a response from 'V', just not in combination with 'Ctrl'. – Eleanor Zimmermann Dec 16 '15 at 01:30
  • I submitted a bug on github; this commit fixes the issue: https://github.com/paperjs/paper.js/commit/a3546e7a79aa3bee66e0f13ef7952c1bc71378c4 – bmacnaughton Dec 20 '15 at 15:03
  • Hi @Scott H if my answer has solved your question please consider accepting it by clicking the check-mark. There is no obligation to do so but it does help people know there is an answer available. – bmacnaughton Dec 30 '15 at 20:16
  • @bmacnaughton Could you update your answer with some information about how Paper.js will handle this in the future based on the changes in that commit? Any idea when that will percolate from boolean-fix to develop to master? – Scotty H Dec 31 '15 at 15:53
  • Is there a reason the answer is not accepted? Paper will handle it by detecting that the browser is Chrome and taking the appropriate actions to make sure the keyup event takes place. I don't have any idea when it will make it to master - Jürg is in the middle of chasing down some really hairy bugs on winding and detecting intersections and overlaps. – bmacnaughton Dec 31 '15 at 16:16

1 Answers1

2

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.

bmacnaughton
  • 4,950
  • 3
  • 27
  • 36