Skip to main content
Ebun.1
Senior
May 19, 2021
Solved

I want to know the X and Y coordinates when the button is pressed

  • May 19, 2021
  • 7 replies
  • 2082 views

Thank you for helping.

At the event of pressing the button of the view class

Is there a way to find out the touched X, Y coordinates?

Thank you.

This topic has been closed for replies.
Best answer by Alexandre RENOUX

Hi Ebun.1,

You cannot do that simply like this.

I would highly recommend you to use handleClickEvent() function and do something like :

void Screen1View::handleClickEvent(const ClickEvent& evt)
{
 if(button1.getAbsoluteRect().intersect(evt.getX(), evt.getY()))
 {
 if(evt.getType() == ClickEvent::PRESSED)
 {
 //retrieve x, y with evt.getX()/evt.getY()
 //Do whatever you want, you can even call other functions
 }
 }
 Screen1ViewBase::handleClickEvent(evt);
}

/Alexandre

7 replies

Alexandre RENOUX
Visitor II
May 20, 2021

Hello Ebun.1,

You simply need to implement the handleClickEvent() function in your view to receive the click events.

void Screen1View::handleClickEvent(const ClickEvent& evt)
{
 touchgfx_printf("Cooordinates of the click event : x = %d/ y = %d\n", evt.getX(), evt.getY());
 Screen1ViewBase::handleClickEvent(evt);
}

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

/Alexandre

Ebun.1
Ebun.1Author
Senior
May 20, 2021

Hi Alexandre RENOUX

Thank you for helping.

I have an interaction when the button is pressed.

How can I get x and y in this?

Screen1View :: pushedButton ()

{

 // get x, y

}

Alexandre RENOUX
Alexandre RENOUXBest answer
Visitor II
May 20, 2021

Hi Ebun.1,

You cannot do that simply like this.

I would highly recommend you to use handleClickEvent() function and do something like :

void Screen1View::handleClickEvent(const ClickEvent& evt)
{
 if(button1.getAbsoluteRect().intersect(evt.getX(), evt.getY()))
 {
 if(evt.getType() == ClickEvent::PRESSED)
 {
 //retrieve x, y with evt.getX()/evt.getY()
 //Do whatever you want, you can even call other functions
 }
 }
 Screen1ViewBase::handleClickEvent(evt);
}

/Alexandre

Ebun.1
Ebun.1Author
Senior
May 20, 2021

Hi Alexandre RENOUX

I am always grateful for your help.

It's not easy, isn't it?

Thank you for your answer.

lagari
Visitor II
September 8, 2021

Could you please explain your solution a little more clearly? I am having the same problem.

Tesla DeLorean
Guru
September 8, 2021

The example looks to be pulling the X and Y coordinates at the event, then determining if those are within the boundaries of the button image on the screen, and then allowing you to take a specific action. If you want to remember the X,Y coordinates save them in a variable or class where you can get at them later, or pass them to a processing function.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
Ebun.1
Ebun.1Author
Senior
September 8, 2021

Hello.

I wanted to know the raw value before the coordinate transformation, so

I decided to save the coordinates in a global variable in BSP_TS_GetState () and use it.

MJohn.10
Visitor II
September 9, 2021

Hey,

you can try this one too, hope it may works for you.

OnTouchListener mOnTouch = new OnTouchListener()

{
 
 @Override
 
 public boolean onTouch(View v, MotionEvent event)
 
 { 
 
 final int action = ev.getAction();
 
 switch (action & MotionEvent.ACTION_MASK) {
 
 case MotionEvent.ACTION_DOWN: {
 
 final int x = (int) ev.getX();
 
 final int y = (int) ev.getY();
 
 break;
 
 }
 
};
 

And if its works and you already got one please mention it too. Thanks.