Loading

Monday, January 14, 2008

Detecting keystrokes

The getSuggest function is supposed to be passed an event object that it will refer to as keyEvent, which holds data about the key event that just took place:

function getSuggest(keyEvent)
{
.
.
.
}

However, this method of passing the event object doesn’t work in the Internet Explorer, which means getSuggest won’t be passed anything in that browser.You have to use the window.event object instead in the Internet Explorer.So the first line of getSuggest is a typical line of JavaScript that uses the JavaScript conditional operator to make sure you have an event object to work with. Here’s an example that
shows how to use this operator:

var temperature = condition ? 72 : 55;

If condition is true, the temperature variable will be assigned the value 72; if condition is false, temperature will be assigned 55. In the getSuggest function, you can use the conditional operator to test whether keyEvent has a non-zero value. If it doesn’t, you should use window.event instead:

function getSuggest(keyEvent)
{
keyEvent = (keyEvent) ? keyEvent: window.event;
.
.
.
}

You can also determine which control the user was typing into, but that depends on which browser the user has. In the Internet Explorer, you use the srcElement property of the keyEvent object, but otherwise, you use the target property to get the control the user was typing into:

function getSuggest(keyEvent)
{
function getSuggest(keyEvent)
{
keyEvent = (keyEvent) ? keyEvent: window.event;
input = (keyEvent.target) ? keyEvent.target :
keyEvent.srcElement; .
.
.
}

Excellent. You have all the data you need about the key event. Now you can
use the following code to check whether the event was a key up event:

function getSuggest(keyEvent)
{
keyEvent = (keyEvent) ? keyEvent: window.event;
input = (keyEvent.target) ? keyEvent.target :
keyEvent.srcElement;
if (keyEvent.type == “keyup”) {
.
.
.
}
}

If the event was a key up event, it’s time to read the struck key. If there is
some text in the text field, it’s time to connect to Google Suggest.

SHARE TWEET

Thank you for reading this article Detecting keystrokes With URL http://x-tutorials.blogspot.com/2008/01/detecting-keystrokes.html. Also a time to read the other articles.

0 comments:

Write your comment for this article Detecting keystrokes above!