Connecting Tech Pros Worldwide Help | Site Map

Associating keys to a function

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 24th, 2005, 12:55 AM
francescomoi@usa.com
Guest
 
Posts: n/a
Default Associating keys to a function

Hi.

I wonder if I can associate some keys (e.g. 'Ctrl+T') to some
function I create with JavaScript on my webpage.

Thank you very much.


  #2  
Old July 24th, 2005, 02:15 AM
Jeff North
Guest
 
Posts: n/a
Default Re: Associating keys to a function

On 23 Jul 2005 17:39:25 -0700, in comp.lang.javascript
francescomoi@usa.com wrote:
[color=blue]
>| Hi.
>|
>| I wonder if I can associate some keys (e.g. 'Ctrl+T') to some
>| function I create with JavaScript on my webpage.
>|
>| Thank you very much.[/color]

You could use the ACCESSKEY attribute but these use the ALT+ sequence.

Google on ACCESSKEY or here is a link with associated CSS for
displaying the shortcut key:
http://www.alistapart.com/articles/accesskeys/
---------------------------------------------------------------
jnorthau@yourpantsyahoo.com.au : Remove your pants to reply
---------------------------------------------------------------
  #3  
Old July 24th, 2005, 02:55 AM
Yann-Erwan Perio
Guest
 
Posts: n/a
Default Re: Associating keys to a function

francescomoi@usa.com wrote:

Hi,
[color=blue]
> I wonder if I can associate some keys (e.g. 'Ctrl+T') to some
> function I create with JavaScript on my webpage.[/color]

This is an interesting problem, which unfortunately cannot be solved in
a cross-browsers way. Not only browsers do not manage key events in the
same way (event type, key codes, modifiers), but also browsers already
attach macros to key shortcuts and might prevent addityional code to be
run (of course these shortcuts differ across user agents).

As a result, if you target the web, then don't even try further and
simply reconsider the way you trigger your functions (for instance only
use buttons) - indeed having something work in one browser is likely to
not work elsewhere, and even potentially breaking a core functionality.

If targeting only one browser (e.g. in an intranet), then it might be
interesting to study its key event model and develop specific code - it
depends on how much time you have:-)

Anyway, just for the sake of the exercise, a short example follows, with
a dirty hack for IE.


---
<script type="text/javascript">
// bind method
var bind=(function(){
var macros=[];

document.onkeypress=function(evt){
evt=evt||window.event;

var k=evt.keyCode||evt.which;
var hasCtrl=evt.ctrlKey ? bind.CTRL : 0;

/*@cc_on if(hasCtrl) k+=0x60; @*/

var key=String.fromCharCode(k).toLowerCase();
for(var ii=0, m; ii<macros.length; ii++) {
m=macros[ii];
if(m.key==key && m.modifiers==hasCtrl){
m.func(evt);
}
}
if(window.focus) window.focus();
}

return function(func, key, modifiers) {
macros[macros.length]={
func:func,
key:key.toLowerCase(),
modifiers:modifiers||0
};
}
})();

bind.CTRL=1;


// test
function test1(){alert("test1");}
function test2(){alert("test2");}

bind(test1, "y");
bind(test2, "y", bind.CTRL);
</script>
---


HTH,
Yep.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.