Skip to main content
December 9, 2020
Solved

How to read touchscreen coordinates when image or box is clicked?

  • December 9, 2020
  • 6 replies
  • 3007 views

Hello!

I want to put on screen box, iamge or other rectangular object.

I want to catch event when this object is clicked and read coordinates of click.

How to do it?

At first i tried read coordinates after clicking button. I added iteraction to button, which start c++ function GET_TOUCH()

#include <STM32TouchController.hpp>
 
void GET_TOUCH()
 
 {
 
 uint32_t x,y; 
 
			// Override and implement this function in OPTIONS_SCREEN
 
			STM32TouchController::sampleTouch(x,y);
 
 }

I got error

"../touchgfx/gui/src/options_screen_screen/options_screenview.cpp(115): error: #245: a nonstatic member reference must be relative to a specific object@"

How to call sampleTouch(x,y) correctly?

This topic has been closed for replies.
Best answer by Michael K

The reason you can't call it like that is just as the error describes: the sampleTouch function isn't static (independent of an object instance). Because there are no references to sampleTouch() anywhere in a TouchGFX created project other than the method in STM32TouchController itself, it's safe to assume that function is called only by the TouchGFX library.

TouchGFX provides a way to get the coordinates of a touch however. You are looking for ClickEvents. A callback with a ClickEvent parameter is generated by any Drawable with the ClickListener mixin.

Example Implementation:

  1. Follow this ClickListener mixin guide (halfway down the page)
  2. In the last code snippet where it says //Implement what should happen when 'box' is touched/clicked here, implement something along the lines of...
if(evt.getType() == ClickEvent::PRESSED){
 int x = evt.getX();
 int y = evt.getY();
 // Do something with x and y, such as...
 presenter->touchRegistered(x, y); // you write this function in the presenter
}

6 replies

Michael K
Michael KBest answer
Senior III
December 10, 2020

The reason you can't call it like that is just as the error describes: the sampleTouch function isn't static (independent of an object instance). Because there are no references to sampleTouch() anywhere in a TouchGFX created project other than the method in STM32TouchController itself, it's safe to assume that function is called only by the TouchGFX library.

TouchGFX provides a way to get the coordinates of a touch however. You are looking for ClickEvents. A callback with a ClickEvent parameter is generated by any Drawable with the ClickListener mixin.

Example Implementation:

  1. Follow this ClickListener mixin guide (halfway down the page)
  2. In the last code snippet where it says //Implement what should happen when 'box' is touched/clicked here, implement something along the lines of...
if(evt.getType() == ClickEvent::PRESSED){
 int x = evt.getX();
 int y = evt.getY();
 // Do something with x and y, such as...
 presenter->touchRegistered(x, y); // you write this function in the presenter
}

Embedded UI/UX Consulting: cadenza.design
December 12, 2020

Sorry, I can't understand something

I get new coordinates each time when I press Box1. But I want to get coordinates each time, when my fingers moves, without releasing and new pressing. Like Slider widget gives new position while sliding.

How?

Michael K
Senior III
December 12, 2020

Does it work if you remove the if(evt.getType() == ClickEvent::PRESSED) condition?

Embedded UI/UX Consulting: cadenza.design
Alexandre RENOUX
Visitor II
December 10, 2020

Hello,

To be able to click an image or a box, add a clickListener to your object (image or box) and write some code callback for the click event.

I enclosed a simple example. If you run simulator, and click on the red box, the green box will change to blue.

When your question is answered, please close this topic by choosing Select as Best.

/Alexandre