Jump to content

2 keyboard events


optikalefx

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.