How to trigger the enter keypress - javascript

I need to know how to trigger an enter key on an input. That is, in response to some other event (not a keypress), I need to trigger a keypress of value 13.
(Clarification, I do not want to trigger some event when the enter key is pressed. If I didn't know how to do that, I could find that that's been asked and answered several times on SO. I want the reverse. I am trying to develop a workaround that requires I emulate an 'enter' keypress.)

You can do this -
var e = $.Event( "keypress", { which: 13 } );
$('#yourInput').trigger(e);
http://api.jquery.com/category/events/event-object/
http://api.jquery.com/trigger/

var e = $.Event( "keyup", { keyCode: 13 } );
$('#yourInput').trigger(e);
Worked for me, instead of 'which', I used 'keyCode'

Simulate enter keypress
var e = jQuery.Event("keypress");
e.which = 13
e.keyCode = 13
$('#email').focus();
$('#email').trigger(e);
capture enter keypress
$('#email').keypress(function (e) {
if (e.which == '13') {
alert('code');
}});

Related

How can I pass two keydown events at the same time with JavaScript?

Is there a way using jQuery or vanilla JS that I can pass down 2 keystrokes?
Example: When I press the space key as an event listener, both the enter and shift key should get pressed and passed down together. I managed for the code to run, but for one single key stroke. Can someone shine some light on this?
$(document).keydown(function(event) {
var keycode = (event.keyCode ? event.keyCode : event.which);
if (keycode == 13) {
document.addEventListener('keydown', function(ev) {
console.log(ev.which);
});
var e = new KeyboardEvent('keydown', {
'keyCode': 190,
'which': 190
});
console.log(e);
document.dispatchEvent(e);
}
});
To add modifiers like shift, ctrl, or alt to a KeyPress event, you can pass those properties in the second constructor argument. See the documentation here.
// ex. Shift+Enter
var e = new KeyboardEvent("keydown", {
key: "Enter",
shiftKey: true,
});

How to trigger a space key event with keycode

I am trying to trigger space key with keycode in JavaScript. I will be sending voice command with space and it should trigger a space event with a keycode.
This is what I have done so far
if(firstword =="space"){
const ev = {
type: 'space',
keyCode: 32
}
editor.triggerOnKeyDown(ev);
The code works perfectly if I use enter or other keycode but not working for space, any idea?
You can try like this:
const ev = new KeyboardEvent('keydown',{'keyCode':32,'which':32});
Reference: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/KeyboardEvent
Ty to create addEventListener for space
window.addEventListener('keypress', function (e) {
if (e.keyCode == 32) {
alert('Space pressed');
}
}, false);

SImulate "Enter pressing" when submit button is clicked

When my submit Button in the form is clicked, I need to simulate that the "Enter button" is pressed. so: clicking on the submit form is equal to pressing Enter on the keyboard. Because I want to have the same method for both...
Here is what I have, but until now it doesnt works..
$(document).on('click', '#send', function(){
$("#chatMessageTextarea").focus();
var e = jQuery.Event("keypress");
e.which = 13;
e.keyCode = 13;
});
try something like this, not tested it, hope it helps!
function keyUpFunc(e) {
var keyCode_Enter = 13;
if (e.keyCode === keyCode_Enter) {
e.preventDefault();
$('#send').trigger("click");
}
}
$(document).keyup(keyUpFunc);
$(document).on('click', '#send', function(){
// Click Event handler for send
});
I am not sure what are you trying to achieve here. But if you want that I think this link has answer to simulate keypress via jQuery.
Definitive way to trigger keypress events with jQuery
Copied code from link:
var e = jQuery.Event("keydown");
e.which = 13; // # Some key code value
$("input").trigger(e);
Try this:
$(document).on('click', '#send', function(){
$("#chatMessageTextarea").trigger({
type: "keypress",
which: 13,
keyCode: 13});
});
hope it helps!

preventDefault not working with tab(9) keyCode

I using a keypress event for enter and tab. But for some reason the code only runs with enter. By pressing tab it just does the default tab action and ignores the code. Please help.
onEnterAddWord: function(ev) {
var kc = ev.which || ev.keyCode;
if (kc === 13 || kc === 9) {
ev.preventDefault();
this.$el.find('.add-word-input input').trigger('blur');
this.$el.find('.viewbox').trigger('click');
console.log('check');
}
},
Try keydown instead of keypress
Modifier and non-printing keys does not fire the keypress event.
How about adding ev.stopImmediatePropagation(); which will prevent other eventListeners to fire?

How to trigger another keycode after catching Enter keycode('13')? [duplicate]

This question already has answers here:
How to catch event.keyCode and change it to another keyCode?
(4 answers)
Closed 9 years ago.
I managed to catch the Enter keycode and prevent my page from Posting.
But I need to add another character in the place of Enter: keycode 39 to be exact.
How can I achieve this? This is my javascript. I see the alert firing but not the following function :(
Enter code
function CkKeyPress(e) {
var evt = (e) ? e : window.event;
var key = (evt.keyCode) ? evt.keyCode : evt.which;
if (key == 13) {
CancelDefault(evt);
}
}
function CancelDefault(e) {
if (e.preventDefault) {
e.preventDefault();
alert('enter caught');
ReplaceEnterWithCharacter();
}
e.returnValue = false;
}
function ReplaceEnterWithCharacter() //this function doesnt work somehow
{
e = $.Event('keyup');
e.keyCode = 65; // A
$('input').trigger(e);
}
<asp:TextBox ID="txtSearch" runat="server" onkeypress="return CkKeyPress(event);"></asp:TextBox>
I've had some success with this kind of thing:
jQuery.event.trigger({ type : 'keypress', which : 65 });
Which triggers the keypress event at the current focus.
I don't think it's possible to change the value of the keycode on an event in mid-trigger, nor is it possible to trigger a keypress event on an arbitrary element even if it is an input or is actually the current focus.

Categories