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

  • Home
  • SEARCH
  • 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 9128499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:31:44+00:00 2026-06-17T07:31:44+00:00

Well I searched on Google but still didn’t found the answer I was looking

  • 0

Well I searched on Google but still didn’t found the answer I was looking for.

I want to check if the user pressed a key, something like this –

if(document.onkeyup) {
   // Some Stuff here
}

I know I can do this, this way –

document.onkeyup = getKey;

But the function getKey cannot return values.

So how can I check if the user pressed a key?

EDIT : I need pure Javascript for this thing..

  • 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-17T07:31:45+00:00Added an answer on June 17, 2026 at 7:31 am

    You can do this in pure Javascript using the event object, without the need of external libraries such as jQuery.

    To capture the keycode, just pass the event as parameter of getKey function:

    function getKey(e)
    {
        window.alert("The key code is: " + e.keyCode);
    }
    
    document.onkeyup = getKey;
    

    Frequently used keyCode list:

    For a usefull list of keyCodes, you can check out this URL:

    http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes

    Setting the keyCode to a global variable:

    If you are interested in capturing the keyCode for later usage, you can do something like this:

    var keycode = "";
    
    (...)
    
    function getKey(e)
    {
        keycode = e.keyCode;
    }
    
    document.onkeyup = getKey;
    
    
    window.alert("The key code is: " + keycode);
    

    Setting the keyCode to the event source object:

    If you don’t like global variables, like me, you could also do something like this:

    function getKey(e)
    {
        keycode = e.keyCode;
    
        var objectFromEvent = e.currentTarget ? e.currentTarget : event.srcElement;
    
        objectFromEvent.customProperty = keycode;
    }
    
    
    document.customProperty = "";
    document.onkeyup = getKey;
    
    // now the value is in the "customProperty" of your object =)
    
    window.alert("The key code is: " + document.customProperty);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I searched a lot on stackoverflow as well as google, but there is no
I searched over google as well as few answers from stackoverflow but none of
I searched around on Google, but I was unable to find any libraries for
I have searched many a page of Google results as well as here on
I've searched google and reviewed answers on StackOverflow, but cannot seem to resolve this
I've searched on Google and Stack Overflow, but I'm unable to resolve this problem.
This property make me confuse. well.. i searched in google: What is the z-index?
I've searched the site thoroughly and Googled for this as well, but to no
I searched in this group and googled around, but still no luck in answers
I've searched Google for about 1,5h now and i can't find a good answer.

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.