←back to thread

293 points ulrischa | 1 comments | | HN request time: 0.233s | source
Show context
marcellus23 ◴[] No.42174016[source]
> All we had to do was change the isInvokedByMouse to check that screenX and screenY don't equal 0, rather than checking if they are greater than 0.

It's obviously extremely unlikely but what if the mouse is actually at 0,0 when the user clicks? I'm not very familiar with JS, is checking for != 0 really the best/only way to do this?

EDIT: actually upon going back, I realized I didn't fully process this sentence originally but it seems to address this:

> We should probably do further refactoring of the event handler function, since it's complicated by the fact that it also handles keydown events. For now, though, this fix will do just fine.

replies(3): >>42174129 #>>42174225 #>>42174311 #
whstl ◴[] No.42174225[source]
It seems that querying the screen position is just a heuristic they came up with to determine the nature of the event. Instinctively I would use instance of MouseEvent for this, but even this feels risky/hacky to me.

My question is why they're relying on those heuristics. My guess is that toggleMenu is being used by multiple event handlers. Or maybe there's something else going on that is specific to their codebase.

It's hard to judge without knowing the full picture.

EDIT: Aha, there's an answer here: https://news.ycombinator.com/item?id=42174177

replies(1): >>42180007 #
1. alain94040 ◴[] No.42180007[source]
You can do a search on GitHub and there is quite a bit of code that is using event.screenX > 0. Maybe someone should file some bug reports.

Also, stack overflow suggests that exact code to "differentiate between mouse and keyboard triggering onclick": https://stackoverflow.com/questions/7465006/differentiate-be...