Page 1 of 1

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

Posted: Sun Dec 30, 2007 12:05 am
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?

Posted: Mon Dec 31, 2007 4:42 pm
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?

Posted: Fri Jan 04, 2008 10:57 am
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?

Posted: Fri Jan 04, 2008 6:23 pm
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

Posted: Sat Jan 05, 2008 5:13 am
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

Posted: Mon Jan 07, 2008 11:25 am
by Mightywayne
Arjan, that's exactly what I needed! Thanks, very very helpful.