Forum Moderators: open

Message Too Old, No Replies

Prevent space/backspace except on form elements?

         

JAB Creations

9:59 am on Mar 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm trying to prevent the spacebar from acting as a second pagedown key and the backspace from acting as a back button. I'm not interested in why I shouldn't opt out of bad design as these keys loose data (backspace when forms loose focus without the user being aware) and break accessibility (the spacebar opens tree menus for those using keyboard only navigation). Additionally it's a toggle option on my site so you can opt out of this. So that is the end of that discussion before it goes any further.

The following code disables the two keys but I am unable to get them enabled on text fields. I'm not having any errors though if I post something on the internet then I'll probably have a breakthrough after I hit the submit button. Some of this code might look familiar to some folks around here. ;)

- John

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>test page</title>
<script type="text/javascript">
function pkeys() {
var pkeysEnabled = true;

if (!pkeysEnabled) {return;}
else {alert('uh hi!');}// end if pkeysEnabled
}

window.onkeydown=function(e){

if (e.keyCode==8 ¦¦ e.keyCode==32) {
return false;
}
}

function pkeysinit() {
// Find all input fields we're interested in
var inputs = document.getElementsByTagName("input");
var selects = document.getElementsByTagName("select");
var texts = document.getElementsByTagName("textarea");
var tags = new Array();

// Push every element into tags
for (var i =0 ; inputs[ i]; i++) tags.push( inputs[ i]);
for (var s =0 ; selects[ s]; s++) tags.push( selects[ s]);
for (var t =0 ; texts[ t]; t++) tags.push( texts[ t]);

// Search all our input fields....
for( var t = 0; t < tags.length; t++) {
var tag = tags[ t]; // Make code a little more legible

// Check it's input text, or SELECT, or TEXTAREA
if( (tag.tagName == "input" && tag.type == "text") ¦¦ (tag.tagName == "input" && tag.type == "password") ¦¦ (tag.tagName == "select") ¦¦ ( tag.tagName == "textarea")) {
addEvent( tag, "focus", powerKeysDisable); // Disable when active
addEvent( tag, "blur", powerKeysEnable); // Enable when not active
}
}

addEvent(document, "keydown", keyPressed);
}

function addEvent(elm, evType, fn, useCapture)
{
//Credit: Function written by Scott Andrews
//(slightly modified)
var ret = 0;

if (elm.addEventListener)
ret = elm.addEventListener(evType, fn, useCapture);
else if (elm.attachEvent)
ret = elm.attachEvent('on' + evType, fn);
else
elm['on' + evType] = fn;

return ret;
}

function start() {
pkeysinit();
}
window.onload = start;

</script>
</head>

<body>

<div style="background-color: #bbb; height: 4000px; width: 100%;">
<p>We want the spacebar to <b>not</b> act as pagedown but enable it to work in input[text/password] and select elements.
The same thing applies with the backspace key though we want to prevent it from acting as a back button.</p>
<form>
<fieldset>
<div><input size="22" type="text" value="123"></div>
<div><input size="22" type="text" value="backspace this"></div>
<div><input size="22" type="text" value="space_the_underscores"></div>
<div><input size="22" type="text" value=""></div>
</fieldset>
</form>
</div>

</body>
</html>

JAB Creations

2:47 pm on Mar 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Resolved :)

- John

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>test page</title>
<script type="text/javascript">

function keyPressed(evt) {

var pkeysEnabled = true;
if (!pkeysEnabled) {
return;
}
else if(document._powerKeysEnabled == true) {
// the focus is NOT on a form element
if (evt.keyCode==8 ¦¦ evt.keyCode==32) {
// don't allow backspaces or spacebars
evt.preventDefault();
return false;
}
}

}//pkeys()

function pkeysinit() {
// Find all input fields we're interested in
var inputs = document.getElementsByTagName("input");
var selects = document.getElementsByTagName("select");
var texts = document.getElementsByTagName("textarea");
var tags = new Array();

// Push every element into tags
for (var i =0 ; inputs[ i]; i++) tags.push( inputs[i]);
for (var s =0 ; selects[ s]; s++) tags.push( selects[ s]);
for (var t =0 ; texts[ t]; t++) tags.push( texts[ t]);

// Search all our input fields....
for( var t = 0; t < tags.length; t++) {
var tag = tags[ t]; // Make code a little more legible

var nodeName = tag.nodeName.toLowerCase();
var type = tag.type.toLowerCase();
// Check it's input text, or SELECT, or TEXTAREA
if( (nodeName == "input" && type == "text") ¦¦ (nodeName == "input" && type == "password") ¦¦ (nodeName == "select") ¦¦ ( nodeName == "textarea")) {
addEvent( tag, "focus", powerKeysDisable); // Disable when active
addEvent( tag, "blur", powerKeysEnable); // Enable when not active
}
}

addEvent(document, "keydown", keyPressed);
// start off ._powerKeysEnabled at true
document._powerKeysEnabled = true;
}

function powerKeysDisable(e) {
// a form element has the focus
document._powerKeysEnabled = false;
}

function powerKeysEnable(e) {
// a form element lost the focus, so we're no where right now
document._powerKeysEnabled = true;
}

function addEvent(elm, evType, fn, useCapture)
{
//Credit: Function written by Scott Andrews
//(slightly modified)
var ret = 0;

if (elm.addEventListener)
ret = elm.addEventListener(evType, fn, useCapture);
else if (elm.attachEvent)
ret = elm.attachEvent('on' + evType, fn);
else
elm['on' + evType] = fn;

return ret;
}

function start() {
pkeysinit();
}
window.onload = start;

</script>
</head>

<body>

<div style="background-color: #bbb; height: 4000px; width: 100%;">
<p>We want the spacebar to <b>not</b> act as pagedown but enable it to work in input[text/password] and select elements.
The same thing applies with the backspace key though we want to prevent it from acting as a back button.</p>
<form>
<fieldset>
<a href="index3.html?file">self anchor</a>
<div><input size="22" type="text" value="123"></div>
<div><input size="22" type="text" value="backspace this"></div>
<div><input size="22" type="text" value="space_the_underscores"></div>
<div><input size="22" type="text" value=""></div>
</fieldset>
</form>
</div>

</body>
</html>