Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7713045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:46:21+00:00 2026-06-01T01:46:21+00:00

I’m trying to do the following, seemingly simple thing. If a key is pressed

  • 0

I’m trying to do the following, seemingly simple thing.

If a key is pressed that would have typed a character in a text input field, and that keypress is not already handled elsewhere (e.g. no input field had the focus), then focus one particular input field and type the character there.

This is to save users a mouse click in a browser game that requires quick responses, but it could equally well be used in, say, a chat application. If the user accidentally forgot to focus this (big and obvious) input field, then I want to handle this gracefully and not punish them.

Caveats:

  • If any input field has focus, the event should go there and nowhere else.
  • Keys that do not type characters (Enter, arrow keys, PgUp, PgDn, …) should be left to do their default behaviour.

Using jQuery, I came up with something like this:

$(document).keypress(function(e) {
  var input = $("#my-input-field");
  if (e.which >= ' ' && !input.is(":focus")) {
    input.focus();
    input.trigger(e);
    e.preventDefault();
  }
});

However, this does not work, for two reasons:

  1. Events still bubble up to document even if a particular input field handles them. Possible solutions:
    • Hook up keypress event handlers to all input fields and call stopPropagation in each, but that sounds like a horrible hack waiting to go wrong.
    • Check in the above handler whether an input field currently has focus, and ignore it if so, but that sounds like I’d be missing some corner cases where something else is using the keypresses.
  2. Calling .trigger(e) does not work. It does call my custom event handlers, but does not trigger the defaut keypress behaviour of actually inserting the character.

What is the cleanest way out?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T01:46:22+00:00Added an answer on June 1, 2026 at 1:46 am

    Thinking about it, the events fire in this order: keydown, keyup, keypress.

    You can use something like:

    $(document.body).on('keydown', function(e) {
        if(document.activeElement.tagName.toLowerCase() != 'input') {
            $('#my-default-input').focus();
        }
    });
    

    Then the keydown event will finish, it will later fire the keyup event (on the now-focused field) and should write the character into the field, and then fire the keypress event that other events (if any) tied to the field can use.

    The contents of the if statement can be changed to filter out certain key presses, or to add other tags, like <canvas> to the “do-not-touch” list.

    Also note that I haven’t tested this, but it theoretically should work. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.