Sansespere Posted January 26, 2009 Share Posted January 26, 2009 How would I specify which key is being pressed in the code. I wouldn't be able to put onshiftdown or something?I want to make my code so that if i hold shift or ctrl or some other key of my choosing, I can get a chosen result. function init () { var links = document.getElementsByTagName("a"); for (var i = 0; links[i]; i++) { links[i].onclick = function rolld() { var sides = this.id.match(/[0-9]+$/); var d=(Math.floor(Math.random()*Number(sides)) + 1); var result = document.getElementById("d"+ sides +"result"); result.innerHTML = d; var x = d/sides;/*I want to begin with an if statement, saying that if I am holding down the Shift key (or whatever i want), that i can change the code to max the output. I can easily do the math and most of the coding, I just simply need to know what the condition coding is for holding down a key, or even better if i can press say, f10 or some random key the condition will be triggered as "on" and will not go "off" until f10 or the key is pressed again. I would like to know both if possible */ if (sides == 3 && d == 1) { result.style.color = "#00d800"; } else if (sides == 3 && d == 2) { result.style.color = "#eeee00"; } else if (sides == 3 && d == 3) { result.style.color = "#d80000"; result.innerHTML += "!"; } else if (sides == 2 && d == 1) { result.style.color = "#00d800"; } else if (sides == 2 && d == 2) { result.style.color = "#d80000"; result.innerHTML += "!"; } else if (x <= .25 && x > 0) { result.style.color = "#00d800"; } else if (x > .25 && x <= .50) { result.style.color = "#eeee00"; } else if (x > .50 && x <= .75) { result.style.color = "#ffaa00"; } else if (x > .75 && x < 1) { result.style.color = "#d80000"; } else if (x == 1) { result.style.color = "#d80000"; result.innerHTML += "!"; } return false; } }}window.onload = init; Any ideas? Thanks Link to comment Share on other sites More sharing options...
jeffman Posted January 26, 2009 Share Posted January 26, 2009 Play around with this. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html> <head> <meta http-equiv="content-type" content="text/html;charset=iso-8859-1"> <title></title> <style type="text/css"> div { width: 4em; float: left; } </style> <script type="text/javascript"> function init () { document.onkeydown = function (e) { e = e || window.event; var k = e.keyCode || e.which; if (k >= 32) { document.getElementById("keys").innerHTML += k + " = " + String.fromCharCode(k) +"<br>"; } if (e.shiftKey) { document.getElementById("modifiers").innerHTML += "Shift<br>"; } if (e.altKey) { document.getElementById("modifiers").innerHTML += "Alt<br>"; } if (e.ctrlKey) { document.getElementById("modifiers").innerHTML += "Control<br>"; } if (e.metaKey) { document.getElementById("modifiers").innerHTML += "Meta<br>"; } } } window.onload = init; </script> </head> <body> <div id="keys"><br></div> <div id="modifiers"><br></div> </body></html> Link to comment Share on other sites More sharing options...
Sansespere Posted January 26, 2009 Author Share Posted January 26, 2009 Ok thanks! I experimented a bit and came up with: document.onkeydown = function (e) { cheat = 0 if (e.shiftKey && cheat == 0) { cheat = 1 } else if (e.shiftKey && cheat == 1) { cheat = 0 } that should allow me to toggle a cheat each time the shift key is pressed. Now I can just add an if statement in the other section of the code using the global cheat variable and that should solve my problem. Thanks! Link to comment Share on other sites More sharing options...
jeffman Posted January 26, 2009 Share Posted January 26, 2009 Be sure to add this line to handle IE's event model:e = e || window.event; Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.