←back to thread

293 points ulrischa | 9 comments | | HN request time: 1.049s | source | bottom
Show context
duxup ◴[] No.42174148[source]
Why are screenX and screenY relevant here as far as their code goes?

Where they using those values in their code?

Very interesting article but I'm missing the step where it would impact their code ...

replies(1): >>42174292 #
1. t43562 ◴[] No.42174292[source]
...because when they are 0 one can infer that the event came from a keypress rather than a mouse. They want to know this.
replies(3): >>42174436 #>>42174868 #>>42175013 #
2. shdon ◴[] No.42174436[source]
Then it would make a lot more sense to check event.pointerType == 'mouse' vs event.pointerType == '' (for keyboard)
replies(2): >>42175068 #>>42175287 #
3. G1N ◴[] No.42174868[source]
Based on the other replies here it seems like it's to differentiate taps vs mouse clicks, keyboard events in js don't have a screenX or screenY property (you can run this in your browser console on this HN post to confirm):

  (() => {
    const logEvent = event => console.log({
      coords: [event.screenX, event.screenY],
      type: event.type
    });
    const input = document.querySelector("textarea");
    // use "keydown" instead of "keypress" to detect all keyboard input instead of just character producing input 
    input.addEventListener("keydown", logEvent);
    input.addEventListener("click", logEvent);
  })();
Type in or click on the reply text input and you'll see that the coords array is undefined for all keyboard events. I haven't tried this equivalent on a touch device however, so not sure how it's handled there.
4. hyperhopper ◴[] No.42175013[source]
This does not work when the mouse is actually at 0,0
replies(1): >>42175496 #
5. ◴[] No.42175068[source]
6. jorams ◴[] No.42175287[source]
That does seem quite obviously better. Even when insisting on checking coordinates why use the coordinate system carried by the event that you have the least control over. Why not .pageX/.pageY, which can't trigger the failure case as long as you make sure the element isn't in the far top left.
7. xboxnolifes ◴[] No.42175496[source]
An imperfect solution for a situation that will practically never happen and have no noticeable downside.
replies(1): >>42179370 #
8. noworriesnate ◴[] No.42179370{3}[source]
It'll screw up UI tests
replies(1): >>42179861 #
9. xboxnolifes ◴[] No.42179861{4}[source]
Clearly not, otherwise this bug would have been found much sooner.