cancel
Showing results for 
Search instead for 
Did you mean: 

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

Ebun.1
Senior

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Alexandre RENOUX
Principal

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

View solution in original post

8 REPLIES 8
Alexandre RENOUX
Principal

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
Senior

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
Principal

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
Senior

Hi Alexandre RENOUX

I am always grateful for your help.

It's not easy, isn't it?

Thank you for your answer.

lagari
Associate

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

Ebun.1
Senior

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.

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 Venmo
Up vote any posts that you find helpful, it shows what's working..
MJohn.10
Associate

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.