Thursday, August 13, 2009

Using Keystrokes in a Web Application

There was an interesting question in the APEX Forum of somebody wanting to use the keyboard to control the screen.

Controlling your screen with the keyboard is a challenge when you are in a web environment as you can be on different platforms and have different browsers that are not always acting the same way.

I created a quick APEX page and included some javascript to enable that page to know what keys are typed from the keyboard. During testing I thought about what would happen with a text item and you want to type something in there... you don't want that something happens then (e.g. submitting the page when you hit S). Luckily in javascript you can control that as well.



<script type="text/javascript">
function checkKeyPress(evt) {
// get events
var evt = (evt) ? evt : ((event) ? event : null);
if (!evt) return true;
// get where you are on the page
var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
// if you are in a text item show this alert otherwise another
if (node.type == "text")
alert('in item:' + evt.keyCode + ': ' + String.fromCharCode(evt.keyCode));
else
alert(evt.keyCode + ': ' + String.fromCharCode(evt.keyCode));
}

// on every Key Down, run the function to check which key was hit
document.onkeydown = checkKeyPress;

</script>

2 comments:

Anonymous said...

This might come in handy. Especially in 4.0 if jquery will be encluded:

http://code.google.com/p/js-hotkeys/

Byte64 said...

Hi Dimitri,
initially i found the navigation with the Mac rather difficult, but then i read the following article and things got definitely better.

Bye!
Flavio