Nativescript: Key Listener support for TextField

Created on 12 Aug 2015  路  13Comments  路  Source: NativeScript/NativeScript

TextField should implement a method for listening for and responding to key presses.

feature high

All 13 comments

Can't this be solved with data bindings?

E.g. creating a binding context:

var observableModule = require("data/observable");
var bindingContext = new observableModule.Observable({
    textFieldInput: ''
});
page.bindingContext = bindingContext;

...attaching it to the TextField:

<TextField text="{{ textFieldInput }}" />

And then reacting to when that that property is changed:

bindingContext.on(Observable.propertyChangeEvent, function (propertyChangeData) {
    console.log(propertyChangeData.propertyName + " has been changed and the new value is: " + propertyChangeData.value);
});

This is the way I do "search-as-you-type". See this search.js of a project of mine (and the corresponding search.xml).

Docs: propertyChangeEvent

Well, that's about what I ended up with also to solve the issue. However, sometimes you need to see that actual key press value. For example, I am working with a Bluetooth bar code scanner. It sends all of its data into the keyboard buffer (it actually registers as a Bluetooth keyboard when you pair it). When it's done sending data it sends the tab character. The tab character doesn't cause the data bound value to change, rather it causes the focus to change to the next control. There isn't a lost focus method or a key press listener for the TextField, so I had to hack it a bit by watching for my bound value to be a certain length. It's not a good solution.

Key listener may help implement "next button" handler
68747470733a2f2f662e636c6f75642e6769746875622e636f6d2f6173736574732f343738323837362f313236313531372f65313637336261612d326332652d313165332d396663392d6332336638646163393833372e6a7067

I will investigate if this can be implemented and how.

Excellent, thanks. One additional thought, most programming languages support key press listening, and also handling. So, if you mark that the event has been handled, it stops it from bubbling up to other listeners. Just a thought while you're looking into it.

More info on the subject: http://stackoverflow.com/questions/1967740/onkeylistener-not-working-with-soft-keyboard-android

So apparently classical key down/up events are not readily supported by the operating systems. I will close this issue now. If someone can invent a reasonable way to implement such a feature for both platforms, please reopen the issue and share your ideas.

It's odd in deed that Android does not require onKeyListener to work with the soft keyboard, however, it is required to work with all physical keyboards. Bluetooth scanners register themselves as keyboards. Therefore, the onKeyListener is still a quite useful method to implement.

I don't develop for IOS natively, but I did find this article.

Here's the docs for how to do it on android.

-Jay

In iOS it is not that straight-forward to respond to a keyboard event and extract the key value. @jkappel71 The article you pointed to uses Apple's private APIs and an app using the approach would get rejected from the store. Generally, the suggested way is to handle the text change notifications of a UITextField instance and to respond to an action like "A text will be inserted at position, do something". In iOS 7+ there is the keyCommands array which would _probably_ help with some special keys but still, these are stated to be applicable to attached hardware keyboards only.

@hamorphis I'm not a very good with native iOS, but still I found UITextFieldTextDidChangeNotification and it works with native apps.

On Android we have TextWatcher from api level 1.

May I know, is there any particular reason for not using them on NS.

@manojdcoder Does the UITextFieldTextDidChangeNotification provide the key being pressed?

@hamorphis As you mentioned already I too don't find the exact option for key being pressed, but still textFieldShouldChangeCharactersInRangeReplacementString delegate method could be a work around. range: NSRange should give us the changed characters through which we assume the pressed keys, returning false will prevent inputs.

This may fail only if user plays with select all / clear / past menus, but in most cases it should behave as expected.

Is there any updates on these feature?

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Leo-lay picture Leo-lay  路  3Comments

hshristov picture hshristov  路  3Comments

valentinstoychev picture valentinstoychev  路  3Comments

rLoka picture rLoka  路  3Comments

tsonevn picture tsonevn  路  3Comments