I wonder if there's any way to detect if the user made a two-finger tap on the MacBook trackpad with JavaScript or jQuery. In one of the projects I'm detecting right mouse click with JavaScript and using it for some actions. It actually doesn't work when you make a double finger tap on a trackpad. While in action that works as a right click, but JavaScript can't detect it like so.
Any clues?
You can use the mouse events button property - MDN reference
A value of 1 would be the indicator of a left click
A value of 2 would be the indicator of a right click.
Demo jsFiddle
Related
I have studied my earlier problem with onmousedown and ontouchstart, and I now realized that with smartphones (at least with android), holding the finger touch pressed without moving normally causes the element to be selected. Whereas, with a mouse, holding the click pressed works perfectly fine.
Now, my problem is that my goal is that the user has to do some effort in order to make a stack grow, and for this purpose, holding the mouse click pressed over time is the effort. So I am thinking now, that maybe a smartphone would ideally have some other design of effort, like moving the finger or something.
Anyway, if I would still continue trying to replicate this mouse-logic with the finger touch, would I have to somehow tell the browser something like 'holding touch pressed does not mean selecting an element/this element'?
Many thanks!
On iOS at least you can use -webkit-user-select: none to prevent this behaviour. I tested this out on an iPhone in person.
To begin, I have read the documentation that says pointerId conveys no meaning
but to some extent it feels like it does.
When retrieving events from pointerdown on a desktop computer you can see that the pointerId property is always 1. I use this to verify if pointerdown event is coming from a mouse click.
However, when I open the Chrome developer tools and use the responsive mode and change it to a mobile device, let's say Pixel 2, and I click on the screen (which is simulated as a touch event) I get an incrementing pointerId on every click.
Now again, I understand it doesn't have a meaning but why does it stay 1 on desktop but go up infinitely on mobile?
I have inserted a simple snippet which when you run on the page it'll show 1 every click and if, you're on a desktop, click on full page and switch to responsive mode and pick a mobile device, it'll show an incrementing value.
window.addEventListener('pointerdown', (event) => {
console.log(event.pointerId);
});
Update
Thanks to the comments I realize that pointerId increments every time because there could be who knows how many fingers touching the screen of a mobile device so there is no way to differentiate them from others.
Why do I need (want) to keep track of pointerId?
Let's say I'm making an application that on desktop only needs the 1 mouse pointer but on mobile it requires 2 fingers to be used to interact with the application.
I need to keep track of where the left finger has been on the screen and what events it has caused and the same for the right finger. The problem comes where the incrementing pointerId comes in is there is no way for me to tell which finger is touching the screen.
Has anyone come across a solution for a problem like this?
How to add touch controls for mobile and mouse click for desktop in aframe.I am getting confusing with this part in aframe is there any help?
Thanks
Try using the mouse-component, the raycasts emitted from the mouse clicks seem to work as well with mobile touch events.
Note, that You may run into multiple issues when You use the a cursor, and the mouse-cursor-component simultaneously. For example it is possible to make a double click ( one with the component, second with the cursor ).
I've been developing a mobile site for my homepage and I have run into an issue when hooking into mobile touchevents. Basically I would like to accomplish the following:
User scrolls down
on touchend event is fired --> a function is called to figure out the amount of the document that is hidden above after the scroll (like jQuery scrollTop)
program takes action based on the amount of the document that is hidden up top
My issues are the following. So touchend works like expected in iOS, when the user lifts her finger the function makes a call to jQuery.scrollTop() which gives me a pixel value for how much the user has scrolled down. However on Android Browser devices it seems that the jQuery.scrollTop() value is calculated on touchstart. That is to say the event doesn't fire off properly, I get the correct pageX & Y coordinates from the touchend event, however scrolltop() returns the value from when the user started scrolling. I've checked around on the inet and this seems to be a known android browser bug, what I want to know is if there a decent workaround for this issue i.e. one that doesn't involve preventing the default scroll behaviour!? Thanks in advance!
Are you taking into account smooth scrolling? or just basic scrolling?
With basic scrolling you should be able to get the correct value simply by using document.body.scrollTop
Let me know if there is an issue
I have coded a jquery script where there is a small grid on screen and using drag and drop users can place tiles on the grid (snaps in place). Currently if you hover over a tile it fades in the option to rotate, but I would much prefer it if you could right click to rotate (making it more natural). I understand blocking right click completely is often frowned upon so was wondering if it was possible just within a particular element, then capturing that event, doing something in JS and disabling the context menu? - that works in every browser.
On a side note, currently I am using jQuery for effects and custom javascript for drag and drop, is it worth looking at a jQuery plugin for drag and drop?
Many thanks,
For capturing the right click, you can use this jquery:
$('#gridID').bind('contextmenu', function(e) {
// do stuff here instead of normal context menu
return false;
});
This works in chrome, firefox, and safari. Haven't tested IE. Works in IE too. Only caveat is it doesn't work in Opera apparently. So if you can live with that...
I'm not a fan of using the right mouse button on web pages. However, if you really want to do it, you could trap the right mouse button as described here. You could block the right mouse button (in other words return false in your event handler) conditionally if the mouse is over your grid cells.
Regarding your bonus question: jquery ui has drag & drop functionality. It's probably easier to use that than rolling your own.
"is it worth looking at a jQuery plugin for drag and drop?"
Only if you don't intend your application to be used on the iPhone O.S with safari, i.e. including iPad, see Safari Web Content Guide: Handling Events