AGuyCalledGerald
AGuyCalledGerald

Reputation: 8150

automatically press keyboard with jQuery?

Is it possible with jQuery to automatically simulate a press on a keyboard, f.ex. inside an html input field?

As explanation: If I press the a inside an input field, an "a" will appear there. I would like to do this not from inside the keyboard, but by pressing a button. I know I could use val("a") on the input but this is not what I want to achieve.

I think trigger() doesn´t help because it only calls the event handler of a keypress event. I do not want to call the handler, but to actively press the keyboard without doing it acually!

Upvotes: 5

Views: 14466

Answers (6)

ori
ori

Reputation: 7847

It seems to me as if the goal is not necessarily to mimic a native browser keypress event, but to manipulate text inside an input field.

If I'm right, you can fetch the .prop('selectionStart') and .prop('selectionEnd') values and overwrite any characters within those indexes by the character(s) you want to "keypress". You can accompany this by triggering of appropriate keyboard events.

I've never worked on text selection before. I know IE handles things differently than normal browsers, so you'll need to look into this topic and find a cross-browser solution if you choose to walk this path. To make things perfect, you'll need to change the selection properties after modifying the input's value as if it were a native paste.

Upvotes: 1

Aleksandar Vucetic
Aleksandar Vucetic

Reputation: 14953

trigger does help, because it fires an event...and that is the only thing you are interested in browser...everything before that is a matter of OS (which you cannot reach, since you are building inside a browser...unless you build some activeX control). So, this should work:

var e = $.Event("keypress");
e.which = <some_key_code>;
$("<your_input_selector>").trigger(e);

As you can see here keypress event is sent when browser registers keyboard input. Difference between keypress and keydown is: "If the user presses and holds a key, a keydown event is triggered once, but separate keypress events are triggered for each inserted character...". You can decide which one you will use depending on your use case.

Upvotes: 4

ShankarSangoli
ShankarSangoli

Reputation: 69915

You can try something like.

$("inputSelector").trigger($.Event("keypress", { keyCode: 97 }));

Pass the keycode of whatever key you want.

Upvotes: 1

pete
pete

Reputation: 25091

Mimicking a keypress in JavaScript (and therefore jQuery) boils down to inserting the "typed" character where you need it and firing off any relevant handlers (i.e., with trigger).

Upvotes: 0

Selvakumar Arumugam
Selvakumar Arumugam

Reputation: 79850

Nope. Not possible in Javascript to simulate actual keyboard key press. Keyboard keypress involves lot more process which is outside Javascript/Browser.

Alternatively you can simulate key press event, but I guess you don't want to call the key press.

Upvotes: 1

RMorrisey
RMorrisey

Reputation: 7739

I don't think there is a javascript API for interacting directly with the keyboard. I would suggest combining a change in the field's value with an event handler firing that would normally correspond to the keyboard press, as you mentioned, so that behavior on the field is triggered as normal.

Upvotes: 0

Related Questions