optikalefx Posted March 5, 2008 Share Posted March 5, 2008 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 } Quote Link to comment https://forums.phpfreaks.com/topic/94406-2-keyboard-events/ Share on other sites More sharing options...
mainewoods Posted March 6, 2008 Share Posted March 6, 2008 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 } Quote Link to comment https://forums.phpfreaks.com/topic/94406-2-keyboard-events/#findComment-485398 Share on other sites More sharing options...
Psycho Posted March 6, 2008 Share Posted March 6, 2008 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> Quote Link to comment https://forums.phpfreaks.com/topic/94406-2-keyboard-events/#findComment-485450 Share on other sites More sharing options...
Psycho Posted March 6, 2008 Share Posted March 6, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/94406-2-keyboard-events/#findComment-485454 Share on other sites More sharing options...
The Little Guy Posted March 7, 2008 Share Posted March 7, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/94406-2-keyboard-events/#findComment-485656 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.