My question is like the subject, is there any example to do this?
How to implement touch event on any widget?
You need to capture the pointer events in the widget. You can then use the contains method to find out if this widget contains the point on screen which has been touched. A while ago, I wrote a listbox which responds to touch events, but there is a bug where the touched widget is wrong, as it doesn't take into consideration any scrolling.
TouchListBox.h
#ifndef _TOUCHLISTBOX_H_
#define _TOUCHLISTBOX_H_
#include <ma.h>
#include <maapi.h>
#include <MAUI/Widget.h>
#include <MAUI/ListBox.h>
#include <MAUtil/Environment.h>
using namespace MAUI;
class ITouchListBoxListener
{
public:
virtual void touchListBoxItemSelected(int selectedIndex) = 0;
};
class TouchListBox : public ListBox, public PointerListener
{
public:
TouchListBox(int x, int y, int width, int height, Widget* parent = NULL);
~TouchListBox();
void pointerPressEvent(MAPoint2d point);
void pointerMoveEvent(MAPoint2d point);
void pointerReleaseEvent(MAPoint2d point);
void setListener(ITouchListBoxListener* listener);
void setEnabled(bool e);
private:
ITouchListBoxListener* l;
void informListener();
void locateItem(MAPoint2d point);
bool enabled;
};
#endif
TouchListBox.cpp
#include "TouchListBox.h"
TouchListBox::TouchListBox(int x, int y, int width, int height, Widget* parent) : ListBox(x, y, width, height, parent, ListBox::LBO_VERTICAL, ListBox::LBA_LINEAR, true)
{
l = NULL;
enabled = false;
}
TouchListBox::~TouchListBox()
{
if(enabled)
{
Environment& e = Environment::getEnvironment();
e.removePointerListener(this);
}
}
void TouchListBox::pointerPressEvent(MAPoint2d point)
{
locateItem(point);
}
void TouchListBox::pointerMoveEvent(MAPoint2d point)
{
locateItem(point);
}
void TouchListBox::pointerReleaseEvent(MAPoint2d point)
{
informListener();
}
void TouchListBox::locateItem(MAPoint2d point)
{
Point p;
p.set(point.x, point.y);
for(int i = 0; i < this->getChildren().size(); i++)
{
if(this->getChildren()[i]->contains(p))
{
this->setSelectedIndex(i);
break;
}
}
}
void TouchListBox::setListener(ITouchListBoxListener *listener)
{
l = listener;
}
void TouchListBox::informListener()
{
if(l != NULL)
l->touchListBoxItemSelected(this->getSelectedIndex());
}
void TouchListBox::setEnabled(bool e)
{
Environment& env = Environment::getEnvironment();
if(!enabled && e)
{
env.addPointerListener(this);
}
else if(enabled && !e)
{
env.removePointerListener(this);
}
enabled = e;
}
@rival: Thanks a lot for ur reply and example code 
Does the emulator support touch events? I'm unsure if I'm using this code correctly (the emulator, using Sony-Ericsson Satio as target) doesn't get my mouse clicks (which I'll assume is the 'touch emulation').
This code tested and works fine on emulator. Please paste some part of ur code so we can check whats wrong with ur code.
The emulator does support touch events. Try the example Moblet/Stylus. You may want to add some logging to setEnabled(), to make sure that addPointerListener() gets called, and to pointerPressEvent(), to see if you actually get any events.
Ok, got it. I've managed to forget my code in less than a month!
I did it in the wrong place (and slightly wrong way
.
Any way... How do I get it to select it (like sending a MAK_{FIRE,SOFTRIGHT,SOFTLEFT} etc) depending on which screen it was clicked on?
For example:
- In my main screen I have a number of buttons (simple layouts) that selects different types of screens
[/*:m] - Two identical input screens with a bunch of EditBox'es, TouchListbox'es with RadioButton choices and a CheckBox.
[/*:m] - A ResultScreen which outputs the mathematical result from the input in/from the input screen(
.
[/*:m] - Two info screens - one basic app info and one help screen[/*:m]
I could solve them both (the last two options have no touch option other than scroll) separately, but not so that TouchListBox::locateItem() can be universal. Not unless I can figure out what screen was/is active - 'screens[x]' where x is the number of the active screen - then I could do a simple switch()...
Also, I couldn't get the RadioButton to be touch'ed, only the Label within the first TouchListBox (the TouchListBox just above the RadioButtonGroup - see below). To illustrate, this is the tree hierarchy of the input screen:
Label
+ TouchListBox
+ Label
+ TouchListBox
+ EditBox
[...]
+ Label
+ TouchListBox
+ RadioButtonGroup
+ RadioButton
+ RadioButton
+ RadioButton
[...]
The 'Label + TouchListBox' combo is a modified version of the createLabel() found elsewhere on the forum. The 'screens' Vector is basically created using 'new Screen'.[/]
Figured I could implement a locateItem() in each screen where I need special treatment (as in the main screen). But I'm still stuck with the createSoftKeyBar().
Can't seem to put two TouchListBox on the same line (?!). I also found some references to a touch/scroll bug...
I have created a Listbox widget with 4 editbox on it.. I would like to trigger different events on clicking different editbox.. and i have done that using pointer press event taking the index and trigerred the event.. but as mentioned by SAm when i scroll the form, the pointer is not recognizing those scrolls.. and conflict occurs in the field press event...how to handle this..
Then another concern is like,,, The same application is working if i never scroll the form and work as it is...
Hence i deployed the same in HTC TyTn based mobile HTC KAIS130. While trying to use the pointer press events, it is incorrectly recognizing the pointer like if i press the third editbox, it is triggering the press event of second editbox.. But the same is working fine in Emulator.
Can anyone help me out on this please.
If you look in the cookbook (http://www.mosync.com/category/tags/cookbook), then you'll find the KineticListBox which will scroll on touch and should correctly identify which widget has been touched. It sounds though that the MS runtime isn't correctly identifying the touch location.