Using button components on a touchscreen

I have created a visual phone keypad in builder with 12 response button components. They are labeled 0-9, backspace and enter. It works great with mouse clicks, but I run into problems when using the touch screen on a Lenovo laptop. The problem is that I have to double-click all the buttons instead of just a single touch like I would like. I have read a bunch of posts about related topics, but they all seem to be aimed at measuring a response and not which button is pushed.

How can I determine which button was touch with a single tap?

If you are experiencing the need to double tap or stroke the buttons on a touchscreen, use the contains method instead of isPressedIn. Have a look at some of my online demos where I record a button press if the button contains the mouse and the mouse coordinates have changed.

Reading what I posted, I don’t think I explained my issue well. I have a bunch of buttons and I want subjects to be able to both single click on them with the mouse or touch them on the touch screen. The button component, without any code, allows subjects to single click them with the mouse or to double tap them on the touch screen.

I looked at a snippet of your code from one of your google docs.

mouseloc = mouse.getPos()
if mouseloc[0]==mouserec[0] and mouseloc[1]==mouserec[1]:
elif rec.contains(mouse):
     if t>minRT:
         continueRoutine = False
          mouserec = mouse.getPos()

That code, as I understand it, is not looking for mouse clicks or touches, but rather only if the mouse is in rec. If the subject touches the screen on rec, the code triggers and everything is perfect. The problem is if the subject uses the mouse and moves through rec on the way to rec2, the code thinks button rec has been pushed. That is not what I want.

I am currently using

mybuttons, mytimes = mouse.getPressed(getTime = True)
if mytimes[0] > 0.0 and rec.contains(mouse):
  continueRoutine = False

If a response click time has been recorded and the mouse is in rec, then the routine moves on. If you are just moving around the mouse, nothing happens since there is no response click. If you touch the screen, a click is recorded and the position is checked.

My concern is the code seems like a hack. The buttons argument returned by the getPressed method does not seem to register a single taps on the touch screen, even though a time is recorded. Because of this, I need to keep dumping past clicks so that simple movement by the mouse does not trigger a button press.

Might the same participant use both a mouse and a touchscreen? If not then you can work out which they have by getting them to press two buttons either side of the screen and seeing whether an invisible rectangle in between is entered by the mouse. Then use isPressedIn or contains as appropriate.

Yes, they can use both and switch whenever they want.

In that case I think you would need to surround your button with a tripwire which switches the behaviour of the button from contains to isPressedIn if the mouse enters it on a trial by trial basis