javascript: capturing function (f1-12) keys [duplicate] - javascript

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Handling key-press events (F1-F12) using JavaScript and jQuery, cross-browser
I am working to replace an old vb6 app with a web app. in the old app the save button was linked to f8 and the users of this application want that to stay the same. How can i capture the f8 button so that it is linked to my save button? Thanks

YOu should be able to bind to the 'keyup' event and look at the keyCode. Here is a list of the keycodes you will need.
http://www.cambiaresearch.com/c4/702b8cd1-e5b0-42e6-83ac-25f0306e3e25/javascript-char-codes-key-codes.aspx

DOM_VK_F8 = 119, so you should check to see that the keypress event listener's event object's keyCode property is equal to 119.
addEventListener('keypress', function(e) {
if (e.keyCode == 119)
; // do stuff here
}, false);

Related

How to set textarea value by jQuery and trigger some event just like user input something [duplicate]

This question already has answers here:
How to trigger event in JavaScript?
(19 answers)
Closed 3 years ago.
For example, I want to set inputbox value in Bing Microsoft Translator
, I can use:
$('#tta_input').val('something')
But translation event are not triggered, it just triggered when I use keyboard input something, so there is any way to trigger translation event when I use $('#tta_input').val('something') to change inputbox text?
So, you need to trigger the keypress event when the text was changed.
Try the following things -
$("#tta_input").on("change paste keyup", function() {
var e = $.Event("keypress", {which: 13});
$(this).trigger(e);
});
To check the code:
$('#tta_input').on('keypress', function(e){
console.log(e.which+' -- KeyPress event Fired');
});
You need to trigger that function on setting value by $('#tta_input').val('something') which you are using for translation on keyboard event.
E.G
$("#tta_input").on("keyup", function(e){
// Your logic of translate
});
And when you setting value by .val() then you should do this
$("#tta_input").val("xyz").trigger('keyup');

Is it possible to disable esc/F11 key during full screen mode of webpage, programmatically? [duplicate]

This question already has answers here:
Prevent esc from exiting fullscreen mode
(2 answers)
Closed 3 years ago.
Is it possible to disable esc/F11 key during full screen mode of webpage, programmatically? I need to have an exit button within the webpage to exit from fullscreen mode, but need to prevent to come outside the fullscreen mode using other funcationality of windows(like esc, F11, etc.). Please help me if anyone have any idea about this.
With jQuery you can disable it listening to the keydown event:
$(document).on("keydown",function(ev){
console.log(ev.keyCode);
if(ev.keyCode===27||ev.keyCode===122) return false
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
keycode 27 stands for the ESC key and 122 for the F11. This was tested in chrome, maybe for other browsers can be other number
document.addEventListener("keydown", e => {
if(e.key == "F11") e.preventDefault();
});

How can I simulate a keypress in JavaScript? [duplicate]

This question already has answers here:
Jquery script for simulated key press down not running keyboard shortcut
(2 answers)
Closed 6 years ago.
I'm trying to find a way to simulate a keypress.
For example, when function launched, the key "Arrow Down" should be pressed and so the webpage should be slightly scrolled.
I'm interested only in Chrome, and both jQuery or plain JS will be appropriate. (Plain JS will be more preferable).
That's one of the code examples I tried:
var e = $.Event("keydown", { keyCode: 40}); // 40 = down arrow
$("body").trigger(e);
// When I launch it the console, nothing happens. The page is not scrolled.
// May be I missed some obvious?
I searched and found the following related questions, but the solutions did not work for me:
Definitive way to trigger keypress events with jQuery
Firing a Keyboard Event in JavaScript
How to trigger event in JavaScript?
Simulate left and right arrow key event with javascript
Simulate Keypress With jQuery
Simulating a Keypress Event from Javascript Console
Simulate JavaScript Key Events
In other words
Using AutoHotkey, you can easily make something like:
Down::
Send, {Up}
Then, if you press Down arrow key, it will be triggered Up. I just want to implement it with JS.
As #rfsbsb pointed out from: Jquery script for simulated key press down not running keyboard shortcut
If you're trying to fire some browser or system wide keyboard shortcut
then it's a dead end - it can't be done for security reasons. If it
would be possible, you would have pages all over the Internet that
would (for example) add themself to your bookmarks without even asking
(by firing CTRL+B shortcut using Javascript).
Using this answer, I managed to change the code a bit and I think I got what you are looking for?
here is my jsfiddle
Code:
jQuery(document).ready(function($) {
$('body').keypress(function(e) {
if(e.which == '40')
$('body').animate({scrollTop: '100px'});
});
});
jQuery.fn.simulateKeyPress = function(character) {
jQuery(this).trigger({
type: 'keypress',
which: character
});
};
setTimeout(function() {
$('body').simulateKeyPress(40);
}, 1000);
Here is a sample starting from #Haring10's example:
https://jsfiddle.net/po33vfb4/4/
$('body').keydown(function(e) {
e.preventDefault();
e.stopImmediatePropagation();
if(e.which == 40) {
window.scrollBy(0, -100);
} else {
window.scrollBy(0, 100);
}
});
Triggering keydown-keyup-keypress programatically does not seem to have the efect of scrolling. The above sample can be customized to add animations.

Trigger event on an alert box [duplicate]

This question already has answers here:
Is there a way to capture the alert ok button click event?
(6 answers)
Closed 8 years ago.
My question is: is there a way to target an element in the alert box, specifically the 'ok' button or which would stimulate the 'enter' key press. I have tried doing this code on the console without any success:
var e = $.Event("keydown", { keyCode: 13});
$("alert").trigger(e);
Popups such as alert halt execution of JavaScript, so you can't listen for an event on them. Better to use a modal popup such as Bootstrap's.

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.

Categories