Software, your way.
burger menu icon
WillMaster

WillMaster > LibraryWeb Page and Site Features

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Key Press Reaction

When a certain key is pressed on a web page, stuff can happen.

As the website owner, you determine which key that is and you determine what will happen. It's done with the JavaScript accompanying presented in this article.

This web page is an example. When the browser window is in focus and you press the lower-case letter "a" on your keyboard, an alert box pops up with the message:

You pressed the keyboard key "a."

For your implementation, perhaps you want to automatically print the page when the letter "p" is pressed. Or reveal a message when the spacebar is pressed.

Any printable letter and space can be specified to react to. The place to specify the letter is marked in the JavaScript. The letter is case-sensitive.

Also in the JavaScript is a function that can be modified for a custom reaction. Currently, it only has an alert() command. That can be removed and replaced with anything a JavaScript function can do, including calling another function if you already have one built.

Here is the JavaScript. Customize it and put it on your web page. It can go anywhere on the page that JavaScript can run, in the HEAD or BODY area. It can also be imported from an external file.

<script type="text/javascript">
/* 
   Key Press Reaction
   Version 1.0
   January 11, 2011

   Will Bontrager
   https://www.willmaster.com/
   Copyright 2011 Bontrager Connection, LLC

   Bontrager Connection, LLC grants you 
   a royalty free license to use or modify 
   this software provided this notice appears 
   on all copies. This software is provided 
   "AS IS," without a warranty of any kind.
*/
////////////// //////////////
// Two places to customize, a variable and a function.
//

// Place 1.
// Specify the keyboard key to react to. It is case sensitive.
//   Only printable letters or space, no function keys.

var KeyboardKey = "a";


// Place 2.
// In this function, tell the JavaScript what to do when 
//   the keyboard key is pressed.
function WhatToDo() {

alert("You pressed the keyboard key \""+KeyboardKey+".\"");

} // end of function WhatToDo()


////////////// //////////////
// No other customizations required in this JavaScript.
////////////// //////////////////////////// //////////////
var pressed;
var key = KeyboardKey.charCodeAt(0);
function handler(e)
{
   if (document.all)
   {
      var keypressed = window.event;
      pressed = keypressed.keyCode;
   }
   else { pressed = e.charCode; }
   if (pressed == key) { WhatToDo(); }
}

if (!document.all)
{
   window.captureEvents(Event.KEYPRESS);
   window.onkeypress = handler;
}
else { document.onkeypress = handler; }
</script>

When the JavaScript is on the page and the browser window is in focus, pressing the specified letter will initiate the specified reaction.

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC