Simulate keystroke "Enter" on a webpage in Chrome / Safari


How can i simulate keystrokes using JS on a webpage in Google Chrome / Safari browsers?
I guess @JMichaelTX has an answer :slight_smile:

AppleScript is simpler to simulate keystrokes


There is an example:

To view the front app but hide all other apps, press Option-Command-H. - Mac Keyboard shortcuts - Apple Support

tell application "System Events" to key code 4 using {option down, command down}

If you insist to use JavaScript, what below may be of help.

JavaScript - Detecting keystrokes

Hey @suliveevil,

I am going to simulate keystroke in background (tab in which i am going to press enter is not frontmost)
that's why i need JS for this action, thanks anyway.

Due to security reasons, you cannot simulate keystrokes using JavaScript.

What you can do in KM, is to Execute a JavaScript in Browser action to setup the web page, then use the KM Type a Keystroke action using RETURN

You can set an option for the Type a Keystroke to go to an app in the background:

Thank you @JMichaelTX

Are you sure that it will work if a desired browser tab is not frontmost?

Could you please provide an example of JS to setup the web page ?

The Browser does not have to be the frontmost app, but the target Tab must be the frontmost Tab in the frontmost Window of the Browser.

Why don't you post the macro you have that does everything but simulate the "ENTER" key. Be sure to put a Comment action where you want the ENTER key to be pressed.

The problem is that i need it to work in a tab which is not frontmost, i can get any information or focus fields from not frontmost tab, but can't simulate a keystroke.

Let me prepare a macro to be posted here.

I don't think you will be able to do this. Think about the actual UI of the Browser -- all keystrokes that a user might use always apply to the frontmost window and tab. You can use AppleScript in lieu of KM, but I'm pretty sure that the keystroke from AppleScript will go to the frontmost window & tab.

Why fight an immovable wall? Why not just temporarily bring the Browser window and tab to front, do your stuff, and then restore the original tab to the front? I've done this several times and it goes so fast you can barely see the window coming to front.

Chris @ccstone may have some thoughts on this.

I am doing other stuff in the same browser window while some macros are running and checking some info (every 5 secs) in several browser tabs which are not frontmost, so i won't be a good idea to activate tabs to simulate keystrokes.

You are right, keystrokes from AS will only go to the frontmost window/tab.

Hey Kirill,

There's just no way to inject bare keystrokes into a tab buried somewhere in your browser.

If you need to hit a confirmation button or something your only option aside from bringing the tab to the front is to discover how to to click your button/widget with JavaScript – IF it's possible.


1 Like

If you can share a screenshot and HTML code for the page, we may be able to offer a suggestion. Many HTML elements support a click() method.

It is a text field which is blank, after focusing the field and pressing "enter" it will be updated, after, it may contain some info what i am looking for. Unfortunately, just clicking the field will do nothing, so the only way is to hit "enter" or refresh the whole page - that is what i am currently doing.

I researched a lot on how to do this stuff using JS, but i am afraid it is not possible.

Guys, many thanks for your interest, we have to close this topic, i will share any info if i will find a something useful to solve this question.

Most likely the ENTER is triggering a Submit Form. If you can identify the form element, then you can use JavaScript to submit().

The field's html code is:

<input name="User[user_id]" type="text" value="">

@kreal, I don't like to work piecemeal. Please post all of the HTML code around the form field in question. Then I'll try to help more.