Detecting "Done /Enter" Key

502 views
Skip to first unread message

gvina...@gmail.com

unread,
Jun 2, 2013, 6:25:47 AM6/2/13
to codenameone...@googlegroups.com
How to know if the user has pressed "Enter/Done" . I tried adding KeyListener to the form and printing "text" but no success. 


f.addKeyListener(13,new ActionListener() {
                                     public void actionPerformed(ActionEvent evt) {
                                         
                                               System.out.println("this action event is performed" + evt);
                                    }
                              });


Shai Almog

unread,
Jun 2, 2013, 2:27:27 PM6/2/13
to codenameone...@googlegroups.com, gvina...@gmail.com
In a text field? Use text field's addActionListener.
If its in a virtual keyboard you won't get key events.
If its in a keyboard the keycode is usually the game key fire.

Vinay Gurram

unread,
Jun 3, 2013, 4:02:54 AM6/3/13
to codenameone...@googlegroups.com, gvina...@gmail.com
Not TextField, detect Enter/Done key event in TextArea. I want to grab the event and change the action to take , if the user presses the Enter/Done key. "Enter/Done" is  ASCII 13 char code, isn't it?. 

Shai Almog

unread,
Jun 3, 2013, 11:53:19 AM6/3/13
to codenameone...@googlegroups.com, gvina...@gmail.com
You can't.
Text area is always implemented entirely in native and so doesn't send key events to Codename One. TextField only sends such events on J2ME.
Either way you shouldn't be doing things like that since they aren't portable as I explained above.

Vinay Gurram

unread,
Jun 5, 2013, 7:59:08 AM6/5/13
to codenameone...@googlegroups.com, gvina...@gmail.com
Okay, I am trying with TextField's AddActionListener . Event is fired every time the focus is lost from the TextField. 

I actually have a data change listener to the text field, which is meant for another action. What I  want to achieve is to do a different action when the user presses the Enter/Done on the device button(only). 

Shai Almog

unread,
Jun 5, 2013, 1:34:46 PM6/5/13
to codenameone...@googlegroups.com, gvina...@gmail.com
We don't currently expose this behavior since this is very device specific.

Vinay Gurram

unread,
Jun 7, 2013, 2:38:20 AM6/7/13
to codenameone...@googlegroups.com, gvina...@gmail.com
But it is a key feature . May be my question is not clear. In googleSearch in mobiles, When I type "search button / Done" it shows the results. But If I don't press "search button/ Done" it shows some suggestions. I want to implement exact  behavior. 

Shai Almog

unread,
Jun 7, 2013, 12:21:08 PM6/7/13
to codenameone...@googlegroups.com, gvina...@gmail.com
For that you should use DataChangeListener on a text field. It won't work on a text area.

Vinay Gurram

unread,
Jun 7, 2013, 1:34:31 PM6/7/13
to codenameone...@googlegroups.com, gvina...@gmail.com
Okay, I am using dataChangeListener. But dataChangeListener is called for every keyPress event. How Do I differentiate If the user presses normal key or Search key?

Chen Fishbein

unread,
Jun 7, 2013, 4:25:48 PM6/7/13
to codenameone...@googlegroups.com, gvina...@gmail.com
as Shai mentioned this is not portable and hard to implement across the platforms, I would suggest you place a search button near your textfield and add you search action to that button. 

Vinay Gurram

unread,
Jun 7, 2013, 5:15:00 PM6/7/13
to codenameone...@googlegroups.com, gvina...@gmail.com
I only want to implement in the Android Version for  now.  Can you please suggest workaround other than having a search button near textField.

Shai Almog

unread,
Jun 8, 2013, 3:40:44 AM6/8/13
to codenameone...@googlegroups.com, gvina...@gmail.com
For the search/done you will get an action event and that is portable.

Vinay Gurram

unread,
Jun 8, 2013, 3:51:04 AM6/8/13
to codenameone...@googlegroups.com, gvina...@gmail.com
How Can I detect that? 
I tried adding actionListener and printed out the evt.getKeyevent().It only got "-1" which is not a keyevent. A small example would be great.

Shai Almog

unread,
Jun 8, 2013, 12:44:11 PM6/8/13
to codenameone...@googlegroups.com, gvina...@gmail.com
I explained previously that there are no key events with a virtual keyboard. You get an action event when the user finishes only then. For the completion use the data change listener, to perform the actual operation use the action listener.

Vinay Gurram

unread,
Jun 9, 2013, 5:12:50 PM6/9/13
to codenameone...@googlegroups.com, gvina...@gmail.com
Yes, I am using the dataChange Listener for the completion . Completion is working good, but the the problem is with the actionListener.  TextField's actionListener is being fired for many events like pressing back button, and others. How do I detecht the actionCame from the Done/Search key not from some other keys?. To narrow down events I tried , searching only if the textField has focus. But events like deviceBack Button and softkeys are invoking the actionListener of the TextField. 

Shai Almog

unread,
Jun 10, 2013, 12:49:09 AM6/10/13
to codenameone...@googlegroups.com, gvina...@gmail.com
We don't make that distinction at the moment. I think there was an RFE a while back to add this but I think it slipped between the cracks.

mcw

unread,
Jun 10, 2013, 10:11:59 AM6/10/13
to codenameone...@googlegroups.com, gvina...@gmail.com

Shai Almog

unread,
Jun 10, 2013, 1:35:32 PM6/10/13
to codenameone...@googlegroups.com, gvina...@gmail.com
Thanks, I pinged Chen about it.

Vinay Gurram

unread,
Jun 11, 2013, 3:24:47 PM6/11/13
to codenameone...@googlegroups.com, gvina...@gmail.com
waiting for this issue to be fixed.

json

unread,
Sep 27, 2017, 1:03:47 AM9/27/17
to CodenameOne Discussions
I wonder if there's any update on this?  or other methods to skin this cat.. I cant seem to make textfield work in android.. my application uses an external barcode scanner as input and for the life of me I couldnt get ActionEvent to run after the text field is populated...  the simulator works really well though.  

Shai Almog

unread,
Sep 28, 2017, 12:14:38 AM9/28/17
to CodenameOne Discussions
This is a post from 2013. Whatever we discussed here is probably irrelevant and has been irrelevant for years...
I suggest phrasing a new question with example of what you did/tried.
Reply all
Reply to author
Forward
0 new messages