Jump to content

Recommended Posts

I want a function to be called when you hit alt q

 

i did this. When using by themeselves works fine, but together, they dont do anything.

 

document.addEventListener('keydown',keyBoardDown, true);

function keyBoardDown(e) {
   if (e['altKey']) alert("alt"); // works
   if (e.keyCode == 81) alert("q"); //works

   if ( (e['altKey']) && (e.keyCode == 81) ) alert("alt q"); //doesnt work
}

Link to comment
https://forums.phpfreaks.com/topic/94406-2-keyboard-events/
Share on other sites

just try alerting what keycode you are actually getting when you do alt q, it should be different than just a straight q:

function keyBoardDown(e) {
   if (e['altKey']) alert("alt"); // works
   if (e.keyCode == 81) alert("q"); //works

    // debug
    alert('mycode= ' + e.keyCode );
   if ( (e['altKey']) && (e.keyCode == 81) ) alert("alt q"); //doesnt work
}

Link to comment
https://forums.phpfreaks.com/topic/94406-2-keyboard-events/#findComment-485398
Share on other sites

I don't think that will work. I took an existing script I have and modified it to determine if Alt-Q was being pressed (note: only workws within the text field). you can modify to suite your needs.

 

Basically it just creates a global variable to detemine if the alt key is up or down. Then whenever the "q" is pressed check if Alt is also down.

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>test</title>
<script language="javascript">

//Set global variable
var altKey = false;

function init() {
    var oinputs = document.getElementsByTagName('input');
    for (var i = 0; i < oinputs.length; i++) {

        oinputs[i].onkeydown = function (e) {
            e = e || event;
            switch (e.keyCode) {
                case 18: // alt
                    // same for the enter button
                    altKey = true;
                    break;
                case 81: // Q
                    // same for the enter button
                    if (altKey) {
                        alert('Alt-q');
                    }
                    break;
                default:
                    // the rest of the keys
                    break;
            }
        }

        oinputs[i].onkeyup = function (e) {
            e = e || event;
            if (e.keyCode == 18) {
                altKey = false;
            }
        }

    }
}

</script>
</head>
<body onload="init()">

<input type="text" name="1"/>

</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/94406-2-keyboard-events/#findComment-485450
Share on other sites

I did find one small bug with the code I posted. When you press "Alt-Q" an alert box comes up. So, if you release the Alt key before cancelling the alert dialog the script thinks that Alt is still down. but, if you are not utilizing an alert at that time it shouldn't be an issue.

Link to comment
https://forums.phpfreaks.com/topic/94406-2-keyboard-events/#findComment-485454
Share on other sites

Im not 100% sure what you are trying to do... but...

 

Maybe an access key would work better for you??

 

Link:

<a href="index.html" accesskey="q">Home</a>

 

Button:

<input type="submit" value="Submit" accesskey="q">

 

http://en.wikipedia.org/wiki/Access_keys

Link to comment
https://forums.phpfreaks.com/topic/94406-2-keyboard-events/#findComment-485656
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.