I want to catch the delete event inside a TextField. So I thought about the keyReleased method which contains the keyCode param.
Is the keyCode value always the same for all mobile phones ? I found that it is -8 ( delete ) on the emulator.
Related
Anyone have any ideas how to handle a long press of a keyboard key in Android WebView. I had assumed I would simply be able to use the KeyboardEvent repeat property (https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/repeat) but when I tested it with the following code, the repeat property returned is always false, despite holding a key down.
document.addEventListener('keydown', logKey);
function logKey(event) {
console.log(event.repeat);
}
The above code works as expected on a desktop browser and returns false once, then true repeatedly, while a key is held down. However in Android WebView, it repeatedly returns false.
Any ideas what the issue is, or an alternative way to handle a long keyboard key press?
Update
To clarify, I'm trying to bind to the long press key event rather than ignore it. I want to trigger a function if a key is held down for a duration of time.
I have a textbox that I would like to prevent the user from entering any letters in. They need to be able to enter numbers.
The textbox has an onkeypressed event already set, and I'm adding logic so that if the user enters a letter, nothing is shown.
Regardless of what I do (cancelbubble, stop propogation, return false), the letter is still getting entered in the text box. I've watched the debugger go over these and still the letter is being entered, its like it is taking place after the fact of the event.
The event handler hook you are looking for is onkeydown:
yourInput.onkeydown = function(e){
var char = String.fromCharCode(e.which); // get the char
return /[0-9]/.test(char); //assert it's a number
}
working demo
Returning false from an event handler attached directly (rather than attachEvent) cancels the event.
Actually once I got home to test onkeydown was doing the same thing, when I edited what was in the text box, it would still add the non-numeric character at the end.
Ended up using onkeyup, that appears to be far enough down the line that it will wipe out what is in the textbox.
I was really looking for the ajax filtered textbox functionality, but couldn't use that because we aren't using actual ajax controls in the application.
I'm sure I'm missing something obvious, but: How do I respond to a keypress event anywhere on the page?
For example, say I had a video player and I wanted to pause it if a user pressed the spacebar at any point. Or, I'm writing a game and want the arrow keys to direct a character, without regard for specific views or subviews.
Defining App.ApplicationView = Ember.View.create({ keyPress: whatever }) doesn't seem to work. I would have thought that all keypress events would bubble up to that.
I guess what I want to do is:
$(document).keypress(function(e) {
alert('You pressed a key!');
});
Except through Ember.
If you are looking for a global keypress your code example above is probably the best solution. The ember application is attached to the body (or a root element you've defined) and would require some sort of focus on the view. For example you could do:
App.ApplicationView = Em.View.extend({
keyUp: function(){
alert(1);
}
});
This will work, but the application view or a child will need to have focus. This is useful, for example, if you wanted to capture a key event of all input fields.
I am using LWUIT package for my j2me application.In my application, i have extends Component class and then draw strings on the component.Now i want to get the key code and then draw string on the component based on the key pressed.how do i know which key is pressed in LWUIT? I want to capture key press event on LWUIT and draw strings on screen.Is this possible in LWUIT?
Is there any way to draw strings on screen without using Component in LWUIT?
You can add a label to a container which is normally how we do things in LWUIT and use the component based UI (see our demos).
You can override keyReleased and do your event handling there, but your component needs to be focusable to receive key events. Alternatively you can bind a key listener to the form or override the forms key callback methods.
How can I monitor the keyboard and determine KeyPress events ?
If you want to monitor key press events for UITextView, then override the UITextViewDelegate's ShouldChangeText() method.
If you want to monitor key press events for UITextField, there's a similar method in UITextFieldDelegate called ShouldChangeCharacters().
Find more info on the usage of these methods here and here.
The ShouldChangeCharacters of UITextField is called BEFORE the text is changed and it is not the same as the Changed event of UITextView. This means that getting the text on the ShouldChangeCharacters or ShouldChangeText method, will allways return the text BEFORE the last key was pressed.
The UITextView has a Changed event like the KeyPress event. UITextField has not such event yet.
A workaround for the UITextField can be found here:
bugzilla.xamarin.com/show_bug.cgi?id=864
NSNotificationCenter.DefaultCenter.AddObserver
(UITextField.TextFieldTextDidChangeNotification, (notification) =>
{
Console.WriteLine ("Character received! {0}", notification.Object ==
TextField);
});