Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Key capture basics

Status
Not open for further replies.

snoopy75

Technical User
Aug 24, 2001
340
US
A function I'm trying to develop is going to be storing a sequence of keys pressed. Unfortunately, I can't find any way to take the keyCode generated by the onKeyUp event and translate it into the actual character pressed. Could anyone point me to a table or function to give me the nudge I need? Thanks!

--Ryan
 
Try this for testing:
Code:
<textarea id="blah"></textarea>
KeyCode: <b><span id="kcode"></span></b>

<script language="javascript">
document.getElementById("blah").onkeydown = kbdHandler;

function kbdHandler( e )
{	if (document.all) e = window.event;
	document.getElementById("kcode").innerHTML = e.keyCode;
}
</script>
 

This is what I have used in the past to check if the key pressed is equal to a certain character:

Code:
if (String.fromCharCode(window.event.keyCode) == 'A') // 'A' Pressed

Hope this helps,
Dan

 
Dan,
Unfortunately, String.fromCharCode runs into problems with the keypad numbers (among other keys), which I also need to keep track of. For example, pressing the '5' key on the keypad returns a lower-case 'e' to that function.

It looks like my only option, then, is to compile my own table of keyCode values using vongrunt's script, and write a function to convert keypresses using that table. Unless anyone's already done this, and wants to beat me to it, I'll post that function when I've got it completed.
 
Okay, here's what I ended up with. I know that anybody could have cobbled this together, and probably quicker than I did, but here it is for anybody else to use.
Code:
function identifyKey(kC) {
	var arrKeys = new Array("Backspace","Tab","Enter","Shift","Ctrl","Alt","PauseBreak","Caps Lock","Esc","Space","PageUp","PageDown","End","Home","LeftArrow","UpArrow","RightArrow","DownArrow","PrintScrn","Insert","Delete","0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","LeftWin","RightWin","RightClick","NumPad0","NumPad1","NumPad2","NumPad3","NumPad4","NumPad5","NumPad6","NumPad7","NumPad8","NumPad9","NumPad*","NumPad+","NumPad-","NumPad.","NumPad/","F1","F2","F3","F4","F5","F6","F7","F8","F9","F10","F11","F12","NumLock","ScrollLock",";","=",",","-",".","/","`","[","\\","]","'");
	var arrKeys2 = new Array("Backspace","Tab","Enter","Shift","Ctrl","Alt","PauseBreak","Caps Lock","Esc","Space","PageUp","PageDown","End","Home","LeftArrow","UpArrow","RightArrow","DownArrow","PrintScrn","Insert","Delete","0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","LeftWin","RightWin","RightClick","0","1","2","3","4","5","6","7","8","9","*","+","-",".","/","F1","F2","F3","F4","F5","F6","F7","F8","F9","F10","F11","F12","NumLock","ScrollLock",";","=",",","-",".","/","`","[","\\","]","'");
	var arrCodes = new Array(8,9,13,16,17,18,19,20,27,32,33,34,35,36,37,38,39,40,44,45,46,48,49,50,51,52,53,54,55,56,57,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,96,97,98,99,100,101,102,103,104,105,106,107,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,144,145,186,187,188,189,190,191,192,219,220,221,222);
	var found = false;
	for (i=0; i<arrCodes.length; i++) {
		if (arrCodes[i] == kC) {
			found = true;
			break;
		}
	}
	if (found) {
		return arrKeys2[i];
	} else {
		return "unknown";
	}
}
I made two versions of arrKeys; one that specifies NumPad keys and one that doesn't. In my case, I need the one that doesn't, because I don't care whether a number was pressed on the keypad or the regular keyboard. I put both versions in, just in case there was anyone that did care about that.

If anyone can improve on this, please do. Thanks to vongrunt and Dan for replying to my question. :)

--Ryan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top