Unable to capture Ctrl+Alt+Arrow key event in jQuery - javascript

For some strange reasons I can't capture Ctrl+Alt+Arrow key combination inside textarea. Is it some sort of system hotkey that is getting swallowed by Windows? Ctrl+Alt+Any Letter and Ctrl+Alt+Shift+Arrow are getting captured fine.
$(document).ready(function() {
$("textarea").bind("keydown", function(event) {
console.log(event);
if(event.altKey && event.ctrlKey && event.which == 38) {
console.log("ctrl+alt+up"); //never triggered
}
});
});
When Ctrl+Alt+Any Letter is pressed I see all 3 events in console. When Ctrl+Alt+Arrow is pressed I see only 2 events (for Ctrl and Alt).
Any ideas?

I've just checked your code and everything works just fine in IE8, Firefox and Chrome.
What browser are you using?
Have you tried using jquery hotkeys plugin ?

Related

text field accepting floating number works fine in chrome not in firefox

I have a function in js which accepts only floating number, in which regex of floating number is being tested
$(document).ready(function () {
$(".validate-foating-value").keypress(function (e) {
if (!/^[0-9]*\.?[0-9]*$/.test($(e.target).val() + String.fromCharCode(e.which))) {
return false;
}
});
});
its working fine in chrome in Firefox as well but problems I am facing through Firefox are
backspace not working
arrow keys not working
shift+arrow keys for selection and Ctrl+A not working
here is the fiddle.
any other work around for FLOATING numbers?
You can hack something like:
$(document).ready(function () {
$(".validate-foating-value").keypress(function (e) {
var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
if (!/^[0-9]*\.?[0-9]*$/.test($(this).val() + String.fromCharCode(keyCode)) && keyCode!=8) {
return false;
}
});
});
Please let me know if it works for you.
Thanks
I changed your Javascript/JQuery code to
$(document).ready(function () {
$(".validate-foating-value").bind('input propertychange', function () {
if (!/^[0-9]*\.?[0-9]*$/.test($(this).val())) {
var currentVal = $(this).val();
$(this).val(currentVal.slice(0,-1));
}
});
});
There is one issue with keypress that's documented here, namely this event is not consistently fired across browsers (for instance backspace does not trigger a keypress on Chrome).
I tried using keyup in place of keypress but I couldn't figure out how to detect the fact that the keyboard event was producing some input. I thought of using the JQuery .change on the input box but this event is not fired every time a key is pressed but only when the elements loses focus.
So in the end I I found a solution in the accepted answer to Validate html text input as it's typed
Please let me know if this helps.
http://jsfiddle.net/user2314737/b85k3zbh/10/

keypress event not generated in chrome but works in ie and ff

This question has been asked plenty of times before, but no answers I have found seem to solve my problem.
From a classic asp page I call a Javascript function
on each of my pages. The point is to fire a search button when a user enters search text and presses enter, rather than clicking on the button, or choosing from the Ajax provided selections.
This works fine in IE and FF, as has been the case for every other question asked along these lines.
Here is the Javascript. Can anybody tell me please how to have it work for Chrome as well as IE and FF ?
Edited following answer form Alexander O'Mara below:
Altered function call in body tag on page to use onkeyup instead of onkeypress - onkeyup="KeyPress(event)"
Altered JS function (also after heeding comments re duplication from others - thanks) as follows:
function KeyPress(e)
{
var ev = e || window.event;
var key = ev.keyCode;
if(window.event) // IE
{
key = e.keyCode;
if (key == 13)
{
window.event.keyCode = 0;
$('#btnSearch').click();
}
}
else if (key == 13)
{
btnSearch.click();
ev.preventDefault();
}
}
It seems to work sometimes and not others, and rarely on chrome currently. Is there a guaranteed way to have it work all the time ?
The main page of my site if you want to try it yourself is www.dvdorchard.com.au, your cursor will be sitting in the search box on arrival - enter a word > 3 chars and press enter, if you stay on the page it didn't work, if you move to the productfound.asp page it worked.
Thanks again.
You are looking for the keyup event (documentation). The keypress event is not consistent across browsers. See this question for information on the differences.
Update:
Since you are using jQuery, you can also remove the onkeyup="KeyPress(event)" attribute for you body, and replace your KeyPress function with the following (replacing the contents with your event handling code).
$(window).keyup(function(e){
/*YOUR CODE HERE*/
});
if(e.keyCode)
{
key= e.keyCode;
}
else
{
key = e.charCode;
}
Fire your event with onkeyup
read more
this should work in chrome. I don't know about other browsers
function code(e) {
e = e || window.event;
return(e.keyCode || e.which);
}
window.onload = function(){
document.onkeypress = function(e){
var key = code(e);
// do something with key
// done doing something with key
key=0
};
};

jQuery plugin not firing correction keydown or idle events

I am programming a jQuery plugin which tracks specific events. I have provided 2 JSFiddle examples for the sanitised code to assist at the end of the question.
I am struggling to fathom why 2 particular events are not firing. The first function tracks when the user triggers the backspace or delete keys within an input or textarea field. The code for this:
// Keydown events
$this.keydown(function (e) {
var keyCode = e.keyCode || e.which;
// Tab key
if (e.keyCode === 9) {
alert('tab key');
} else if (e.keyCode === 8 || e.keyCode === 46) { // Backspace and Delete keys
if ($this.val() !== '') {
alert('Backspace or delete key');
}
}
});
I only wish to track the error-correction keys when a field is not empty. The tab key in the above example works as expected within the conditional statement. The backspace and delete keys do not work when inside the plugin and targeting the element in focus.
The second event not firing is tracking whether a user becomes idle. It is making use of jQuery idle timer plugin to manipulate the element in focus.
// Idle event
$this.focus(function() {
$this.idleTimer(3000).bind('idle.idleTimer', function() {
alert('Gone idle');
});
}).focusout(function() {
$this.idleTimer('destroy');
});
With both of these events I have refactored the code. They were outside of the plugin and targeted $('input, select, textarea') and worked as expected. I have brought them inside the plugin, and set them to $(this) to manipulate elements currently in focus. For most of the functions, this has worked without fault, but these 2 are proving problematic.
The first JSFiddle is with the 2 functions inside the plugin. tab works, whereas the correction keys do not. Strangely, in this example the idle function is firing (it does not in my dev environment). As this is working in the JSFiddle, I accept this may be difficult to resolve. Perhaps suggestions on handling an external plugin within my own to remedy this?
Fiddle 1
The second JSFiddle has taken the backspace and delete key functionality outside of the plugin and targets $('input, select, textarea') and now works.
Fiddle 2
For Fiddle1:
if ($this.val() !== '') {
alert('Backspace or delete key');
}
Look at what $this actually is.

jQuery Click Not Working in Safari

For some reason, this script isn't working in Safari (tested on Windows, think it happens on Mac, too, though):
$("#searchTerms").focus(function() {
$(document).keypress(function(e) {
if(e.which == 13) {
$("#searchBtn img").click();
}
});
});
jsFiddle: http://jsfiddle.net/ux86V/
The script is supposed to click an image when a user presses enter while focused on a search box (it has to be set up this way, it's tied in to some weird third party service).
EDIT: It doesn't appear to work at all in the jsFiddle, but it does, so don't just assume the entire script is bad. I think jSFiddle just prevents redirects, and I have it set up to redirect to google.com for the example.
EDIT 2: It appears to be an issue with .click(). Is there an alternative to this that I could use, or is .click() the only way to register a click on an element?
EDIT 3: After more testing, it seems like the jQuery click event is somehow not working properly. It may have something to do with the way the form is submitted, I'm not sure. Link to live demo: http://www.weblinxinc.com/beta/blue-sky-marketing/demo/
13 is the code of enter key which is a special key , you can catch it on keyup only
try to use trigger();
$("#searchTerms").focus(function() {
$(document).keyup(function(e) {
if(e.which == 13) {
$("#searchBtn img").trigger("click");
}
});
});​

Disable Internet Explorer shortcut keys

EDIT: After waited a while and didn't get anything yet, I've decided
to do shortcut disable thingy only for
IE now. Is there a possibility to disable
IE shortcut keys to access menus/print
etc. via vbscript?
Is it possible to disable browser shortkeys?
Because many of them are using in application. For instance, Ctrl+p is using and I don't want browser to popup the print window.
Yes, you can listen for the various key combinations with javascript and disable the default behaviors. There's even a library that you can use and test here. I just tested it using google chrome and firefox in their demo textarea, and it works as you want.
shortcut.add("Ctrl+P",function() {
return;
});
This works in the browsers that I listed above, but IE will not allow you to override the default behavior in some cases.
Your only option in IE is to disable the Ctrl key entirely with something like:
document.onkeydown = function () {
if (event.keyCode == 17) alert('Ctrl Key is disabled');
};
Which is not ideal and probably not what you want, but it will work.
You can try creating an event handler for keydown event, check on the keyCode and prevent its default action if needed. However this will not work in all browsers.
An example for Firefox (canceling "Print" short key, verified):
document.addEventListener("keydown", function(oEvent) {
if (oEvent.keyCode == 80 && oEvent.ctrlKey)
oEvent.preventDefault();
}, false)
There is a nice trick to fight with IE10+, to avoid display browser menus on alt key combinations, like Alt + F, Alt + H ...
I recently used on IE11, just add an anchor with the attribute accesskey:[yourKey] on your body
<body>
<script type="text/javascript">
window.onkeydown = function(e){
console.log(e.keyCode + " alt: " + e.altKey);
e.preventDefault();
};
window.onkeyup = function(e){
console.log(e.keyCode + " alt: " + e.altKey);
e.preventDefault();
};
</script>
</body>
Now when you press Alt + f the browser will not display "File popup" as usual, and will let events keydown and keyup gets to you, and not only keydown.
I am working on similar problem, hooking keyboard event Below code works well to disable, except the flash object on the IE has not got the focus. Since I am trying to handle keyboard event on the flash object, this code does not work for me.
function hookKeyboardEvents(e) {
// get key code
var key_code = (window.event) ? event.keyCode : e.which;
// case :if it is IE event
if (window.event)
{
if (!event.shiftKey && !event.ctrlKey) {
window.event.returnValue = null;
event.keyCode = 0;
}
}
// case: if it is firefox event
else
e.preventDefault();
}
window.document.onkeydown = hookKeyboardEvents;
From you application after calling the method on Ctrl+P just make the keycode as zero.I think that will solve your problem...
window.event.keyCode=0;
this will set the keycode as zero..So when explorer checks for the keyCode it will be zero...so default function will not execute...
Try this...just a suggestion
This works for me in IE 8. The important part is IE requires ev.returnValue to be set to false. NOTE: this only works if you have focus on some element on the document...that is, if you just load the page and hit 'ctrl-p' you'll see the print dialog. But if you click somewhere on the page, then try it, it should suppress the print dialog.
document.onkeydown = function (e) {
var ev = e||window.event;
// Do what I want keys to do ...
// Block browser short cuts
if(ev.preventDefault) // non-IE browsers
ev.preventDefault();
else // IE Only
ev.returnValue = false;
};

Categories