Archive for the 'Javascript' Category

Javascript, Programming

How To Identify a Javascript Keycode

I recently made some modifications to a client’s website that involved using key presses to trigger certain events. I had the numeric keycode for a few of the keys that were already in use, but I didn’t really have a good way of identifying the keyCode for the additional key events I wanted to make use of.

Googling for the result left me with a lot of links that had incorrect information. I searched a little more and found a small Javascript snippit that was supposed to tell you what the keycode was when you pressed down a key, but I found it to be incomplete.

I took that snippit and expanded it to work in IE and Firefox. I’ve included the source here, but you can also check out the working demo.

Here is the source for my little keycode function. Keep in mind that I am using jQuery to update the input box with the correct keycode value.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<html>
<head>
<title>Demo :: How To Identify a Javascript Keycode</title>
<script type="text/javascript" src="jquery-1.2.3.js"></script>
<script type="text/javascript">
function keyCheck(evt){
	key = (evt.which) ? evt.which : evt.keyCode
	$("#keyCode").val(key)
	//alert("KeyCode = "+key)
}
</script>
</head>
<body onKeyPress="keyCheck(event)">
	<p>Press any key to see it's numeric keycode.</p>
	<p>Numeric Key Code: <input type="text" length="5" id="keyCode" name="keyCode"/></p></body>
</html>

Javascript, Programming

Sniff Your Users Version of Flash with Javascript

I ran across this on a website I hit this morning and found it to be quite useful. So I am reposting it here in case anyone else is trying to do this. In a nutshell, this bit of javascript will return a decimal value of the highest version of the Flash Player installed on an end users system. You can even specify the minimum version you are looking for and get a boolean TRUE or FALSE in return. Very slick.

[code]// If called with no parameters this function returns a floating point value
// which should be the version of the Flash Player or 0.0
// ex: Flash Player 7r14 returns 7.14
// If called with reqMajorVer, reqMinorVer, reqRevision returns true if that version or greater is available
function DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision)
{
reqVer = parseFloat(reqMajorVer + “.” + reqRevision);
// loop backwards through the versions until we find the newest version
for (i=25;i>0;i–) {
if (isIE && isWin && !isOpera) {
versionStr = VBGetSwfVer(i);
} else {
versionStr = JSGetSwfVer(i);
}
if (versionStr == -1 ) {
return false;
} else if (versionStr != 0) {
if(isIE && isWin && !isOpera) {
tempArray = versionStr.split(” “);
tempString = tempArray[1];
versionArray = tempString .split(”,”);
} else {
versionArray = versionStr.split(”.”);
}
versionMajor = versionArray[0];
versionMinor = versionArray[1];
versionRevision = versionArray[2];

versionString = versionMajor + “.” + versionRevision; // 7.0r24 == 7.24
versionNum = parseFloat(versionString);
// is the major.revision >= requested major.revision AND the minor version >= requested minor
if ( (versionMajor > reqMajorVer) && (versionNum >= reqVer) ) {
return true;
} else {
return ((versionNum >= reqVer && versionMinor >= reqMinorVer) ? true : false );
}
}
}
return (reqVer ? false : 0.0);

}[/code]