keycode 13 and keycode 10 not working - javascript

Event keypress 13 and 10 not working on iPhone safari, android firefox but working with android default browser.
I have a jsp page which has a form which that takes a number as input and changes values of other div elements in the same page by dividing those numbers by this input.
I am using keypress function of jQuery and testing with keycode 10 and 13 for this. It's working on all desktop browsers but the GO button doesn't fire on safari and firefox on smart phones. Please let me know how to go about this?
Here is the partial code I have used:
$('.number').keypress(function(e) {
if(e.keyCode == 10 || e.keyCode == 13 ) {
$('#1').html((textreplace/input).toFixed(0)+'g');
$('#2').html((textreplace2/input).toFixed(0)+'%');
$('#3').html((textreplace3/input).toFixed(0)+'g');
...
..
}
}
The ".number" is the class name used in the input form which has type="text". So, basically I am not using submit at all.

Try using keyup and keydown. keypress event is not really ideal.

Related

iPad Bluetooth keyboard returns keycode of 0 for any key with onKeyUp

Some clients have been reporting issues when using their iPad Bluetooth keyboards for entering text into one of our internal sites. Mainly pressing enter on a certain input would work fine when using desktop or the iPad on screen keyboard, but not when using a Bluetooth keyboard connected to the iPad.
Upon investigation it appears that any input to an onKeyUp returns 0 as the keycode when connected to a Bluetooth keyboard on the iPad. The demo works fine, however when using the onscreen keyboard it doesn't work because of the keycode returning 0. I created this jsFiddle to demonstrate. It was tested on both Chrome and Safari for iPad with the same results of working fine with onKeyPress but returning only 0 with onKeyUp.
$('#inputKeyUp').keyup(function (event){
$("#outputKeyUp").text("Key Up Key: " + event.which);
});
$('#inputKeyPress').keypress(function (event){
$("#outputKeyPress").text("Key Press Key: " + event.which);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="inputKeyUp">keyup</textarea>
<div id="outputKeyUp">Key Up Key:</div>
<b/>
<textarea id="inputKeyPress">keypress</textarea>
<div id="outputKeyPress">Key Press Key:</div>
EDIT: just reported the bug to Apple. We will see if anything comes of it.
Testing study
I did some testing on this just now and discovered that on the keyUp event when using a Bluetooth keyboard on iOS Safari, the only keys that give any sort of proper feedback in terms of the properties e.key, e.charCode, e.keyCode and e.which are the following keys:
Escape
Up arrow
Left arrow
Right arrow
Down arrow
All other keys return the following:
{
key: "Dead",
charCode: 0,
keyCode: 0,
which: 0
}
These special keys (escape and arrow keys) only return a different value on the e.key property according to the syntax UIKeyInput{PascalCasedKeyName}:
UIKeyInputEscape
UIKeyInputUpArrow
UIKeyInputLeftArrow
UIKeyInputRightArrow
UIKeyInputDownArrow
Summary
On iOS, the only keys you can identify on the keyUp event, based on my quick study, are Escape and the four Arrow keys, by matching their name on the e.key property. These values also appear on the keyDown event.
If you still need to wait until the keyUp event fires for your applications, and you need to match keys other than these special ones, the only solution I can come up with is to use a keyDown event for capturing the key, but then listen for the keyUp event inside that keyDown event like so:
el.addEventListener("keydown", e => {
if (e.which === 13) // Enter key, or which ever key code you'd like
el.addEventListener("keyup", function keyUp(e) {
el.removeEventListener("keyup", keyUp, false) // Memory clean-up
// Your code here
}, false)
}, false)
Furthermore
After a quick search for "UIKeyInput" I discovered that UIKeyInput is "a set of methods a subclass of UIResponder uses to implement simple text entry". (Apple's Developer Documentation) This would explain the special syntax of these key names.
This is a workaround for the enter key in the keyup event.
if (event.type === 'keyup') {
//this is to handle the enter button on an ipad bluetooth keyboard
if (event.key === 'Enter') {
event.which = event.keyCode = 13;
}
}

Detect event.keyCode in Atom editor for Korean/Japanese input

Is there any way to detect a keyCode for onkeydown event for Korean and Japanese keyboard input in Atom editor?
This is really exotic, but I need to detect which key was pressed when user input is set to Hangul (Korean) or Anthy (Japanese) keyboard input.
Any other standard keyboard layout input gives desired key code (q = 81, etc.), but these input modes always posting 229 as event keyCode. This is Atom event subscriber I use:
// ...
editorView.addEventListener('keydown', (event) => {
console.log(event.keyCode);
});
// ...
And it works perfect for other keyboard layouts.
Another thing, I wrote native JS in HTML test page (out of Atom):
document.onkeydown = function(event) {
console.log(event.keyCode);
};
And in Chrome browser (which is I suppose behind Atom) it gives the correct keyCode even for Korean and Japanese input! Which means issue is Atom-specific.

How to detect pressed key on BlackBerry using JQuery Mobile?

I am using JQuery Mobile to developing my own app for BlackBerry based on WebWorks(HTML5,CSS3,JS) tecknologies.
On my layout div i have 3 input elements.
I am wont to detect BlackBerry enter key pressed in first input to focus on next input.
How i can check if hardware enter key(and other keys like "T","R","D") pressed using JQuery Mobile??
There's nothing specific to jQuery Mobile for this. You just need to add an event listener on the input and listen to the appropriate keypress event. You can find a nice explanation for them here.
Here's some sample code to get you started (not tested, you might need to debug):
$('#firstInput').keypress(function(event) {
if ( event.which === 13 ) {
$('#secondInput').focus();
}
);
In the event.which, the key code gets checked (13 is for enter). Other keys have different key codes. Be careful, I remember a bug on Blackberry that it wouldn't trigger the keypress event for backspace.
Be more specific about your problem if you need more details.

How can I prevent the browser's default history back action for the backspace button with JavaScript?

Is there a way to prevent the default action from occurring when the user presses backspace in a browser?
I don't need to prevent the user from leaving, just from having the default backspace action. I need the backspace to do something different (it's a game).
I tried without success:
window.addEventListener('keydown', function(e) {
if (e.keyCode === Game.Key.BACK_SPACE)
{
e.preventDefault();
e.stopPropagation();
return false;
}
}, false);
If I put an alert inside the if, the alert will be shown for backspace key press. So, the keyCode is correct.
This has to work in Opera 10.6, Firefox 4, Chrome 6, Internet Explorer 9 and Safari 5.
You don't need return false or e.stopPropagation(); neither will make any difference in a listener attached with addEventListener. Your code won't work in Opera, which only allows you to suppress the default browser behaviour in the keypress event, or IE <= 8, which doesn't support addEventListener. The following should work in all browsers, so long as you don't already have keydown and keypress event handlers on the document.
EDIT: It also now filters out events that originated from an <input> or <textarea> element:
function suppressBackspace(evt) {
evt = evt || window.event;
var target = evt.target || evt.srcElement;
if (evt.keyCode == 8 && !/input|textarea/i.test(target.nodeName)) {
return false;
}
}
document.onkeydown = suppressBackspace;
document.onkeypress = suppressBackspace;
If you prefer to simply have the fix for yourself, without affecting other users when scripting into the web page, read below.
Here's some solutions that only change the browser you are using:
- Firefox on Linux "unmapped" the backspace behavior since 2006 so it's not affected; (at any rate, it was simply set to scroll up before then)
- Chrome has just announced that it will do the same from now on; (http://forums.theregister.co.uk/forum/1/2016/05/20/chrome_deletes_backspace/)
- Firefox on Windows can be set to ignore backspace by going into about:config and changing the backspace_action setting to 2; (http://kb.mozillazine.org/Browser.backspace_action)
- Safari ?!
I found at Telerik's page script ready to use. Script blocks back button action: by clicking in browser back button and backspace on page. This script works. I'm using it in my project.
http://www.telerik.com/community/code-library/aspnet-ajax/general/disable-backspace-from-master-page.aspx

auto repeat the up/down arrow key?

in firefox when im inside a input textbox and press down the up/down arrow key it doesn't autorepeat. how can i make that happen and control how many keypress it will fire up per sec?
UPDATE: i use:
$('#search_view #search').live('keydown', function(event) {
if(event.keyCode == 40) {
// code
}
});
but it just execute the code one time..i would like it to repeat when holding down the down-arrow.
Use .keydown() instead ..
quote from jQuery .keypress()
In addition, modifier keys (such as
Shift) cause keydown events but not
keypress events.
Arrows fall do not fall in the same category as Shift, but are treated in a special way ... the keydown will do the trick ..
Update
After your comment here is a sample that works in
FF 3.5.x and 3.0.11
IE 6, 7
Google Chrome 4.0.x
Safari 4.0.4
It only does not work on Opera (Edit: works on Opera 12.16) but it does not work with any key .. not just the arrows..
About the rate, you can not alter it from your code.. it is a system option (from BIOS and from keyboard settings in control panel -windows- )

Categories