# DuaL Touch ANDRIOD

**URL:** <https://discourse.libsdl.org/t/dual-touch-andriod/21349>\
**Category:** SDL Development\
**Created:** [April 25, 2015, 6:33am UTC](https://discourse.libsdl.org/t/dual-touch-andriod/21349 "2015-04-25T06:33:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Timodor](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/t/c0e974/32.png) [@Timodor](https://discourse.libsdl.org/u/Timodor)\
**Post date:** [April 25, 2015, 6:33am UTC](https://discourse.libsdl.org/t/dual-touch-andriod/21349/1 "2015-04-25T06:33:12Z")

</div>

I’ve had this running for while now, but noticed about 5% of time it bugs out. You get odd movements.

It seems like I’m doing this wrong.

finger returns sometimes number that are not 0 or 1, thats basically when the problem occurs.

Anyone know simple way to redo this code?

if (event.type == SDL\_FINGERDOWN)  
{  
if (event.tfinger.fingerId == 0)  
{  
bf1 = true;  
iMouseX = Display0-\>GetScreenWidth() \* event.tfinger.x;  
iMouseY = Display0-\>GetScreenHeight() \* event.tfinger.y;  
}  
else if (event.tfinger.fingerId == 1)  
{  
bf2 = true;  
iMouseX2 = Display0-\>GetScreenWidth() \* event.tfinger.x;  
iMouseY2 = Display0-\>GetScreenHeight() \* event.tfinger.y;  
bProcess = true;  
}  
}

---

<div class="post-metadata">

**Author:** ![Liang\_Ying-Ruei](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/liang_ying-ruei/32/2655_2.png) [@Liang\_Ying-Ruei](https://discourse.libsdl.org/u/Liang_Ying-Ruei)\
**Post date:** [April 25, 2015, 7:55am UTC](https://discourse.libsdl.org/t/dual-touch-andriod/21349/2 "2015-04-25T07:55:42Z")

</div>

Maybe you could describe your problem and goal more specific, so we can  
give you suggestion.–

* * *

## \< Work hard, also play hard and have fun \>

```
    \ ^__^
     \ (oo)\ _______
        (__)\ )\/\
            ||----w |
            || ||

```

-------------- next part --------------  
A non-text attachment was scrubbed…  
Name: signature.asc  
Type: application/pgp-signature  
Size: 473 bytes  
Desc: not available  
URL: [http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20150425/c0e38760/attachment.pgp](http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20150425/c0e38760/attachment.pgp)

---

<div class="post-metadata">

**Author:** ![Daniel1985](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/d/a5b964/32.png) [@Daniel1985](https://discourse.libsdl.org/u/Daniel1985)\
**Post date:** [April 25, 2015, 10:05am UTC](https://discourse.libsdl.org/t/dual-touch-andriod/21349/3 "2015-04-25T10:05:05Z")

</div>

With that code you’re only able to process one finger at a time. You’re using a if- and else if-statement, which means that, whenever the first finger (fingerid 0) is held, the else if-statement is never executed. The only time the else if-statement is executed is when the first finger is not held. Try changing that code into this:

Code:

if(event.type == SDL\_FINGERDOWN)  
{  
int NumFingersHeld = 0;

```
// Vector2D - a class or struct specifying a 2D vector object
Vector2D ScreenSize = Display0.GetScreenSize();

if(event.tfinger.fingerId == 0)
{
	// Don't know what this do but I'll leave it here if you need it
	bf1 = true;
	
	iMouseX = ScreenSize.x * event.tfinger.x;
	iMouseY = ScreenSize.y * event.tfinger.y;

	++NumFingersHeld;
}

if(event.tfinger.fingerId == 1)
{
	// Don't know what this do but I'll leave it here if you need it
	bf2 = true;

	iMouseX2 = ScreenSize.x * event.tfinger.x;
	iMouseY2 = ScreenSize.y * event.tfinger.y;

	// Don't know what this do but I'll leave it here if you need it
	bProcess = true;

	++NumFingersHeld;
}

// Only executed when at least 2 fingers is held on the touch panel
if(NumFingersHeld >= 2)
{
	// Do stuff
}

```

}

Note: I returned the screen size as a 2D-vector (mathematical vector, not an std::vector) so the width and height doesn’t need to be “Get’ed” 2 times each, but that’s only optional.

---

<div class="post-metadata">

**Author:** ![Timodor](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/t/c0e974/32.png) [@Timodor](https://discourse.libsdl.org/u/Timodor)\
**Post date:** [April 25, 2015, 12:25pm UTC](https://discourse.libsdl.org/t/dual-touch-andriod/21349/4 "2015-04-25T12:25:19Z")

</div>

I think you fixed it!, I can see the logic error, I need to face palm myself.

Both need to processed.

Thanks Dude, May the Universe give you want you truly desire.

😃😃😃😃😃😃😃😃

---

<div class="post-metadata">

**Author:** ![Timodor](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/t/c0e974/32.png) [@Timodor](https://discourse.libsdl.org/u/Timodor)\
**Post date:** [April 25, 2015, 2:47pm UTC](https://discourse.libsdl.org/t/dual-touch-andriod/21349/5 "2015-04-25T14:47:22Z")

</div>

Much Better Now,

But case event.tfinger.fingerId returns ints and floats. Still a few random pauses. But much better then before. Just been testing it the last 30 minutes.

Naith wrote:\> With that code you’re only able to process one finger at a time. You’re using a if- and else if-statement, which means that, whenever the first finger (fingerid 0) is held, the else if-statement is never executed. The only time the else if-statement is executed is when the first finger is not held. Try changing that code into this:

> Code:
> 
> if(event.type == SDL\_FINGERDOWN)  
> {  
> int NumFingersHeld = 0;
> 
> // Vector2D - a class or struct specifying a 2D vector object  
> Vector2D ScreenSize = Display0.GetScreenSize();
> 
> if(event.tfinger.fingerId == 0)  
> {  
> // Don’t know what this do but I’ll leave it here if you need it  
> bf1 = true;
> 
> ```
> iMouseX = ScreenSize.x * event.tfinger.x;
> iMouseY = ScreenSize.y * event.tfinger.y;
> 
> ++NumFingersHeld;
> 
> ```
> 
> }
> 
> if(event.tfinger.fingerId == 1)  
> {  
> // Don’t know what this do but I’ll leave it here if you need it  
> bf2 = true;
> 
> ```
> iMouseX2 = ScreenSize.x * event.tfinger.x;
> iMouseY2 = ScreenSize.y * event.tfinger.y;
> 
> // Don't know what this do but I'll leave it here if you need it
> bProcess = true;
> 
> ++NumFingersHeld;
> 
> ```
> 
> }
> 
> // Only executed when at least 2 fingers is held on the touch panel  
> if(NumFingersHeld \>= 2)  
> {  
> // Do stuff  
> }  
> }
> 
> Note: I returned the screen size as a 2D-vector (mathematical vector, not an std::vector) so the width and height doesn’t need to be “Get’ed” 2 times each, but that’s only optional.

---

<div class="post-metadata">

**Author:** ![JonnyD](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/jonnyd/32/1604_2.png) [@JonnyD](https://discourse.libsdl.org/u/JonnyD)\
**Post date:** [April 25, 2015, 3:04pm UTC](https://discourse.libsdl.org/t/dual-touch-andriod/21349/6 "2015-04-25T15:04:58Z")

</div>

Since this is in the finger down event handling, I don’t think the “else  
if” construct is wrong at all.

It may be the case that you are getting fingerId values higher than 1. Can  
you give us any more details about what you are trying to do or provide  
more substantial code?

Jonny DOn Sat, Apr 25, 2015 at 10:47 AM, Timodor wrote:

> Much Better Now,
> 
> But case event.tfinger.fingerId returns ints and floats. Still a few  
> random pauses. But much better then before. Just been testing it the last  
> 30 minutes.
> 
> Naith wrote:
> 
> With that code you’re only able to process one finger at a time. You’re  
> using a if- and else if-statement, which means that, whenever the first  
> finger (fingerid 0) is held, the else if-statement is never executed. The  
> only time the else if-statement is executed is when the first finger is  
> not held. Try changing that code into this:
> 
> Code:
> 
> if(event.type == SDL\_FINGERDOWN)  
> {  
> int NumFingersHeld = 0;
> 
> // Vector2D - a class or struct specifying a 2D vector object  
> Vector2D ScreenSize = Display0.GetScreenSize();
> 
> if(event.tfinger.fingerId == 0)  
> {  
> // Don’t know what this do but I’ll leave it here if you need it  
> bf1 = true;
> 
> ```
> iMouseX = ScreenSize.x * event.tfinger.x;
> iMouseY = ScreenSize.y * event.tfinger.y;
> 
> ++NumFingersHeld;
> 
> ```
> 
> }
> 
> if(event.tfinger.fingerId == 1)  
> {  
> // Don’t know what this do but I’ll leave it here if you need it  
> bf2 = true;
> 
> ```
> iMouseX2 = ScreenSize.x * event.tfinger.x;
> iMouseY2 = ScreenSize.y * event.tfinger.y;
> 
> // Don't know what this do but I'll leave it here if you need it
> bProcess = true;
> 
> ++NumFingersHeld;
> 
> ```
> 
> }
> 
> // Only executed when at least 2 fingers is held on the touch panel  
> if(NumFingersHeld \>= 2)  
> {  
> // Do stuff  
> }  
> }
> 
> Note: I returned the screen size as a 2D-vector (mathematical vector, not  
> an std::vector) so the width and height doesn’t need to be “Get’ed” 2 times  
> each, but that’s only optional.
> 
> * * *
> 
> SDL mailing list  
> SDL at [lists.libsdl.org](http://lists.libsdl.org)  
> [http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)

---

<div class="post-metadata">

**Author:** ![Timodor](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/t/c0e974/32.png) [@Timodor](https://discourse.libsdl.org/u/Timodor)\
**Post date:** [April 26, 2015, 12:27am UTC](https://discourse.libsdl.org/t/dual-touch-andriod/21349/7 "2015-04-26T00:27:43Z")

</div>

Hey Jonny, look time no chat.

It’s the same issue I was going on about months ago. I got it work, it’s been working and works 95% of time. But trying to get rid of the bugs.

This is core of it, and this works 95% of time, but I think the issue “event.tfinger.fingerId == 0 or 1” this returns other values other then 0 or 1. touch 3 would return 2.

But sometimes it returns odd numbers. Depending on circumstance, which I think is what is causing these pause’s.

while ( SDL\_PollEvent(&event) )  
{

```
if (event.type == SDL_FINGERDOWN)
{	
	if (event.tfinger.fingerId == 0)
	{
		iMouseX = Display0->GetScreenWidth() * event.tfinger.x;
		iMouseY = Display0->GetScreenHeight() * event.tfinger.y;
	}

	if (event.tfinger.fingerId == 1)
	{
		iMouseX2 = Display0->GetScreenWidth() * event.tfinger.x;
		iMouseY2 = Display0->GetScreenHeight() * event.tfinger.y;
	}
}

if (event.type == SDL_FINGERMOTION)
{
	if (event.tfinger.fingerId == 0)
	{		
		iMouseX = Display0->GetScreenWidth() * event.tfinger.x;
		iMouseY = Display0->GetScreenHeight() * event.tfinger.y;
	}

	if (event.tfinger.fingerId == 1)
	{
		iMouseX2 = Display0->GetScreenWidth() * event.tfinger.x;
		iMouseY2 = Display0->GetScreenHeight() * event.tfinger.y;
	}
}

if (event.tfinger.fingerId == 1)
{
	bf2 = false; // false
	iMouseX2 = -1;
	iMouseY2 = -1;
}

if (bif (event.type == SDL_FINGERUP)
{
	if (event.tfinger.fingerId == 0 )
	{
		bf1 = false; // trure
		iMouseX = -1;
		iMouseY = -1;
	}

	bf1 == false && bf2 == false)
	{
		bFingerDown = false;
	}

```

Jonny D wrote:\> Since this is in the finger down event handling, I don’t think the “else if” construct is wrong at all.

> It may be the case that you are getting fingerId values higher than 1.?? Can you give us any more details about what you are trying to do or provide more substantial code?
> 
> Jonny D
> 
> On Sat, Apr 25, 2015 at 10:47 AM, Timodor \<@Timodor (@Timodor)\> wrote:
> 
> > ```
> > Much Better Now, 
> > 
> > ```
> > 
> > But case event.tfinger.fingerId returns ints and floats. Still a few random pauses. But much better then before. Just been testing it the last 30 minutes.
> > 
> > ```
> > Naith wrote:
> > 
> > With that code you're only able to process one finger at a time. You're using a if- and else if-statement, which means that, whenever the first finger (fingerid 0) is held, the else if-statement is never executed. The only time the else if-statement is executed is when the first finger is not held. Try changing that code into this:
> > 
> > Code:
> > 
> > ```
> > 
> > if(event.type == SDL\_FINGERDOWN)  
> > {  
> > ?? ??int NumFingersHeld = 0;
> > 
> > ?? ??// Vector2D - a class or struct specifying a 2D vector object  
> > ?? ??Vector2D ScreenSize = Display0.GetScreenSize();
> > 
> > ?? ??if(event.tfinger.fingerId == 0)  
> > ?? ??{  
> > ?? ??? ??// Don’t know what this do but I’ll leave it here if you need it  
> > ?? ??? ??bf1 = true;  
> > ?? ??? ??  
> > ?? ??? ??iMouseX = ScreenSize.x \* event.tfinger.x;  
> > ?? ??? ??iMouseY = ScreenSize.y \* event.tfinger.y;
> > 
> > ?? ??? ??++NumFingersHeld;  
> > ?? ??}
> > 
> > ?? ??if(event.tfinger.fingerId == 1)  
> > ?? ??{  
> > ?? ??? ??// Don’t know what this do but I’ll leave it here if you need it  
> > ?? ??? ??bf2 = true;
> > 
> > ?? ??? ??iMouseX2 = ScreenSize.x \* event.tfinger.x;  
> > ?? ??? ??iMouseY2 = ScreenSize.y \* event.tfinger.y;
> > 
> > ?? ??? ??// Don’t know what this do but I’ll leave it here if you need it  
> > ?? ??? ??bProcess = true;
> > 
> > ?? ??? ??++NumFingersHeld;  
> > ?? ??}
> > 
> > ?? ??// Only executed when at least 2 fingers is held on the touch panel  
> > ?? ??if(NumFingersHeld \>= 2)  
> > ?? ??{  
> > ?? ??? ??// Do stuff  
> > ?? ??}  
> > }
> > 
> > Note: I returned the screen size as a 2D-vector (mathematical vector, not an std::vector) so the width and height doesn’t need to be “Get’ed” 2 times each, but that’s only optional.
> > 
> > * * *
> > 
> > SDL mailing list  
> > SDL at [lists.libsdl.org](http://lists.libsdl.org) (SDL at [lists.libsdl.org](http://lists.libsdl.org))  
> > [http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org) ([http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org))

---

<div class="post-metadata">

**Author:** ![JonnyD](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/jonnyd/32/1604_2.png) [@JonnyD](https://discourse.libsdl.org/u/JonnyD)\
**Post date:** [April 26, 2015, 3:47am UTC](https://discourse.libsdl.org/t/dual-touch-andriod/21349/8 "2015-04-26T03:47:49Z")

</div>

I’d suggest you not try to support 2 touches and consider rather how you’d  
support any number of touches. If all you need to know about touches is  
whether they are active and what the position is, then consider storing  
this in a map:  
std::map\<SDL\_FingerID, std::pair\<int, int\> \> touches;

When you get an SDL\_FINGERDOWN, add a touch to the map:  
touches.insert(std::make\_pair(event.tfinger.fingerId, std::make\_pair(x,  
y)));

When you get an SDL\_FINGERUP, erase the id from the map:  
touches.erase(event.tfinger.fingerId);

Motion events can be handled by std:🗺:find and updating the position.

> From this point, you can easily limit input to two touches as needed.

Jonny DOn Saturday, April 25, 2015, Timodor wrote:

> Hey Jonny, look time no chat.
> 
> It’s the same issue I was going on about months ago. I got it work, it’s  
> been working and works 95% of time. But trying to get rid of the bugs.
> 
> This is core of it, and this works 95% of time, but I think the issue  
> “event.tfinger.fingerId == 0 or 1” this returns other values other then 0  
> or 1. touch 3 would return 2.
> 
> But sometimes it returns odd numbers. Depending on circumstance, which I  
> think is what is causing these pause’s.
> 
> while ( SDL\_PollEvent(&event) )  
> {
> 
> if (event.type == SDL\_FINGERDOWN)  
> {  
> if (event.tfinger.fingerId == 0)  
> {  
> iMouseX = Display0-\>GetScreenWidth() \* event.tfinger.x;  
> iMouseY = Display0-\>GetScreenHeight() \* event.tfinger.y;  
> }
> 
> if (event.tfinger.fingerId == 1)  
> {  
> iMouseX2 = Display0-\>GetScreenWidth() \* event.tfinger.x;  
> iMouseY2 = Display0-\>GetScreenHeight() \* event.tfinger.y;  
> }  
> }
> 
> if (event.type == SDL\_FINGERMOTION)  
> {  
> if (event.tfinger.fingerId == 0)  
> {  
> iMouseX = Display0-\>GetScreenWidth() \* event.tfinger.x;  
> iMouseY = Display0-\>GetScreenHeight() \* event.tfinger.y;  
> }
> 
> if (event.tfinger.fingerId == 1)  
> {  
> iMouseX2 = Display0-\>GetScreenWidth() \* event.tfinger.x;  
> iMouseY2 = Display0-\>GetScreenHeight() \* event.tfinger.y;  
> }  
> }
> 
> if (event.tfinger.fingerId == 1)  
> {  
> bf2 = false; // false  
> iMouseX2 = -1;  
> iMouseY2 = -1;  
> }
> 
> if (bif (event.type == SDL\_FINGERUP)  
> {  
> if (event.tfinger.fingerId == 0 )  
> {  
> bf1 = false; // trure  
> iMouseX = -1;  
> iMouseY = -1;  
> }
> 
> bf1 == false && bf2 == false)  
> {  
> bFingerDown = false;  
> }
> 
> Jonny D wrote:
> 
> Since this is in the finger down event handling, I don’t think the “else  
> if” construct is wrong at all.
> 
> It may be the case that you are getting fingerId values higher than 1.?  
> Can you give us any more details about what you are trying to do or provide  
> more substantial code?
> 
> Jonny D
> 
> On Sat, Apr 25, 2015 at 10:47 AM, Timodor \<\> wrote:
> 
> Quote:
> 
> Much Better Now,
> 
> But case event.tfinger.fingerId returns ints and floats. Still a few  
> random pauses. But much better then before. Just been testing it the last  
> 30 minutes.
> 
> Naith wrote:
> 
> With that code you’re only able to process one finger at a time. You’re  
> using a if- and else if-statement, which means that, whenever the first  
> finger (fingerid 0) is held, the else if-statement is never executed. The  
> only time the else if-statement is executed is when the first finger is not  
> held. Try changing that code into this:
> 
> Code:
> 
> if(event.type == SDL\_FINGERDOWN)  
> {  
> ? ? int NumFingersHeld = 0;
> 
> ? ? // Vector2D - a class or struct specifying a 2D vector object  
> ? ? Vector2D ScreenSize = Display0.GetScreenSize();
> 
> ? ? if(event.tfinger.fingerId == 0)  
> ? ? {  
> ? ? ? ? // Don’t know what this do but I’ll leave it here if you need it  
> ? ? ? ? bf1 = true;  
> ? ? ? ?  
> ? ? ? ? iMouseX = ScreenSize.x \* event.tfinger.x;  
> ? ? ? ? iMouseY = ScreenSize.y \* event.tfinger.y;
> 
> ? ? ? ? ++NumFingersHeld;  
> ? ? }
> 
> ? ? if(event.tfinger.fingerId == 1)  
> ? ? {  
> ? ? ? ? // Don’t know what this do but I’ll leave it here if you need it  
> ? ? ? ? bf2 = true;
> 
> ? ? ? ? iMouseX2 = ScreenSize.x \* event.tfinger.x;  
> ? ? ? ? iMouseY2 = ScreenSize.y \* event.tfinger.y;
> 
> ? ? ? ? // Don’t know what this do but I’ll leave it here if you need it  
> ? ? ? ? bProcess = true;
> 
> ? ? ? ? ++NumFingersHeld;  
> ? ? }
> 
> ? ? // Only executed when at least 2 fingers is held on the touch panel  
> ? ? if(NumFingersHeld \>= 2)  
> ? ? {  
> ? ? ? ? // Do stuff  
> ? ? }  
> }
> 
> Note: I returned the screen size as a 2D-vector (mathematical vector, not  
> an std::vector) so the width and height doesn’t need to be “Get’ed” 2 times  
> each, but that’s only optional.
> 
> * * *
> 
> SDL mailing list
> 
> [http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)
