cancel
Showing results for 
Search instead for 
Did you mean: 

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

EEuge
Senior

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Michael K
Senior III

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
}

View solution in original post

6 REPLIES 6
Michael K
Senior III

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
}

Alexandre RENOUX
Principal

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

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?

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

No. If I remove this condition, I get coordinates twice - after press and after release.

I've never used these so I can't comment on it as a solution to your problem necessarily but you might want to look into DragEvents. Good luck, and let me know if it works!