A solution for cramped fingers... or, making elements do...

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

A solution for cramped fingers... or, making elements do...

Post by Mightywayne »

Yeah hi, I have no idea how to phrase the topic title.

Basically I have a game in which people click to go places, and users who play for a bit can get irritated. I remember a forum software employing something where the users would hit alt + enter and instead of dragging their mouse to the submit key, it would just do that action.

Does anyone have any idea remotely of how to do this?
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Post by andym01480 »

Do you mean this? http://www.w3.org/TR/html4/interact/for ... #h-17.11.1

using the tab key to get between elements of a form?
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

Post by Mightywayne »

Erm, no... I figure this would be done with AJAX or javascript or something. Just somehow script a key combination to I guess work as a tabbing feature?
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

You could add a handle to the document's onkeypress, onkeyup and/or onkeydown event. And in the handle check for the event.keyCode, event.altKey and event.ctrlKey properties.

http://developer.mozilla.org/en/docs/DOM:event
User avatar
arjan.top
Forum Contributor
Posts: 305
Joined: Sun Oct 14, 2007 4:36 am
Location: Hoče, Slovenia

Post by arjan.top »

so you need key shortcuts?

This I wrote a while ago:

Code: Select all

 var shortcut_list = {    'n' : function () { document.getElementById('pressed').innerHTML += "pressed n<br />"; },    'm' : function () { document.getElementById('pressed').innerHTML += "pressed m<br />"; },    's+1' : function () { document.getElementById('pressed').innerHTML += "pressed s+1<br />"; },    's+2' : function () { document.getElementById('pressed').innerHTML += "pressed s+2<br />"; },    'f' : function () { document.getElementById('pressed').innerHTML += "pressed f<br />"; },    'f+g' : function () { document.getElementById('pressed').innerHTML += "pressed f+g<br />"; },    'f+g+a' : function () { document.getElementById('pressed').innerHTML += "pressed f+g+a<br />"; }}; var shortcut_setname = {    37 : 'leftarrow',    38 : 'uparrow',    39 : 'rightarrow',    40 : 'downarrow',    32 : 'space'}; //ignore in operavar shortcut_ignore = {    16 : 'shift',    17 : 'ctrl'} var ignore_tags = new Array(    'input'); var shortcut_pressed_list = new Array();var shortcut_pressed = new Array();var shortcut_old = false; function shortcuts(){    if(document.attachEvent){        //set event listener for ie        document.attachEvent('onkeypress', get_shortcut);        document.attachEvent('onkeyup', run_shortcut);    } else if(document.addEventListener){        //set event listener for other browsers        document.addEventListener('keypress', get_shortcut, false);        document.addEventListener('keyup', run_shortcut, false);    }} function get_shortcut(event){        //set event for ie    if(!event) { var event = window.event }        //get pressed key    if(event.charCode){        var key_code = parseInt(event.charCode);    } else if(event.keyCode){        var key_code = parseInt(event.keyCode);    } else if(event.which){        var key_code = parseInt(event.which);    }        //convert key code to character    if(String.fromCharCode(key_code) && !shortcut_setname[key_code] && !shortcut_ignore[key_code]){        key_code = String.fromCharCode(key_code);    } else if (shortcut_setname[key_code] && !shortcut_ignore[key_code]) {        key_code = shortcut_setname[key_code];    }        //check if ctrl or shift key is pressed    if(event.ctrlKey == true){        var shortcut_button = 'ctrl';        if(!shortcut_pressed_list[shortcut_button]){            shortcut_pressed_list[shortcut_button] = shortcut_button;            shortcut_pressed.push(shortcut_button);        }    } else if(event.shiftKey == true){        var shortcut_button = 'shift';        if(!shortcut_pressed_list[shortcut_button]){            shortcut_pressed_list[shortcut_button] = shortcut_button;            shortcut_pressed.push(shortcut_button);        }    }        //add key to pressed buttons array    if(!shortcut_pressed_list[key_code] && !shortcut_ignore[key_code]){        shortcut_pressed_list[key_code] = key_code;        shortcut_pressed.push(key_code);    }    } function run_shortcut(){        if(shortcut_pressed.length > 1){                var shortcut = shortcut_pressed.join("+");         if(shortcut_list[shortcut]){            shortcut_list[shortcut]();        }                shortcut_pressed_list.pop();        shortcut_pressed.pop();         shortcut_old = true;     } else if(shortcut_pressed.length == 1 && !shortcut_old) {         var shortcut = shortcut_pressed.join();         if(shortcut_list[shortcut]){            shortcut_list[shortcut]();        }                shortcut_pressed_list = new Array();        shortcut_pressed = new Array();                     //shortcut_old = true;     } else if (shortcut_old == true){         shortcut_pressed_list = new Array();        shortcut_pressed = new Array();                shortcut_old = false;     }    } 
Note that all the keys wont work cross-browser (in-browser shortcuts) and it havent been tested in opera and ie for a while (but should work)
When writing in a form or in textarea you should disable shortcuts

Almost any key combination should work (ctrl+key, shift+key, key+key)
I dont recommend shift+key and ctrl+key because of cross-browser incompatibilities
Last edited by arjan.top on Sat Jan 19, 2008 12:32 pm, edited 1 time in total.
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

Post by Mightywayne »

Arjan, that's exactly what I needed! Thanks, very very helpful.
Post Reply