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 8541131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:45:39+00:00 2026-06-11T11:45:39+00:00

I’m working yet on a small script to capture keyevents and easily bind functions

  • 0

I’m working yet on a small script to capture keyevents and easily bind functions on them. But I’m stuck for now!

The problem is, that if I initalize more than one “eventhandler” it overides arguments from the first initialization.

Thousands of words do not say more than some lines of code. So, here’s what I’ve done so far:

var keyCodes={a:65,b:66,c:67,d:68,e:69,f:70,g:71,h:72,i:73,j:74,k:75,l:76,m:77,n:78,o:79,p:80,q:81,r:82,s:83,t:84,u:85,v:86,w:87,x:88,y:89,z:90,"0":48,"1":49,"2":50,"3":51,"4":52,"5":53,"6":54,"7":55,"8":56,"9":57,f1:112,f2:113,f3:114,f4:115,f5:116,f6:117,f7:118,f8:119,f9:120,f10:121,f11:122,f12:123,shift:16,ctrl:17,control:17,alt:18,option:18,opt:18,cmd:224,command:224,fn:255,"function":255,backspace:8,"delete":8,enter:13,"return":13,left:37,up:38,right:39,down:40};

var keyCall = function(k, fn, c, e, k2, nk) {
    this.onkeydown = function() {
        nk = k.split(" ");
        c = e ? e.which : event.keyCode;
        if (nk.length > 1) {
            if (keyCodes[nk[0]] === c) {
                k2 = true;
            }
            if (keyCodes[nk[1]] === c && k2 === true) {
                fn();
                k2 = false;
            }
        } else if (keyCodes[nk[0]] === c) {
            fn();
        }
    };
};


keyCall('ctrl a', function() { // overridden by `keyCall('shift a'`
    alert('callback1');
});

keyCall('shift a', function() {
    alert('callback2');
});

I’ve optimized it for a highly minimum of bytes, if you think an annotated version is usefull, so just say it! For everyone who needs, here’s a fiddle

Update

pascalfree has fixed it! But anoter problem is now coming up…

The “placeholder”-variable k2 checks if the first key was/is pressed. But for now if you press and release ctrl and hit thereafter the a-key the function will fired. Any ideas?

  • 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-11T11:45:40+00:00Added an answer on June 11, 2026 at 11:45 am

    I assume the onKeyPress event is bound to some html element like this:

    keyCall.apply( window.body, ['shift a', function() { alert('a') }] );

    In order to append additional events you can check if an eventhandler already is defined and call it in your new function:

    var keyCall = function(k, fn, c, e, k2, nk) {
        var oldOnkeydown = this.onkeydown; //get old event handler
        this.onkeydown = function() {
            if( typeof oldOnkeydown == "function" ) { oldOnkeydown() } //call in new eventhandler.
            nk = k.split(" ");
            c = e ? e.which : event.keyCode;
            if (nk.length > 1) {
                if (keyCodes[nk[0]] === c) {
                    k2 = true;
                }
                if (keyCodes[nk[1]] === c && k2 === true) {
                    fn();
                    k2 = false;
                }
            } else if (keyCodes[nk[0]] === c) {
                fn();
            }
        };
        //reset k2 onkeyup
        var oldOnkeyup = this.onkeyup;
        this.onkeyup = function() {
          if( typeof oldOnkeyup == "function" ) { oldOnkeyup() }
          k2 = false;
        };
    };
    

    I only added the 2 lines with comments. (+ onkeyup event at the end to solve problem from comments)

    Source: http://www.webreference.com/programming/javascript/onloads/index.html

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary
This could be a duplicate question, but I have no idea what search terms

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.