473,386 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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.

Jul 24 '05 #1
2 1467
On 23 Jul 2005 17:39:25 -0700, in comp.lang.javascript
fr**********@usa.com wrote:
| 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.


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/
---------------------------------------------------------------
jn******@yourpantsyahoo.com.au : Remove your pants to reply
---------------------------------------------------------------
Jul 24 '05 #2
fr**********@usa.com wrote:

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


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.
Jul 24 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: vpcs | last post by:
I am trying to do an invoicing software with perl - cgi. I am having a problem in associating the checkbox with the items from the database. I am giving the user option to enter the customer...
2
by: Dave Smithz | last post by:
Hello, Is there a way of associating what colour the hyperlinks should be within a particular class? Let me elaborate: I am using programming to generate HTML code. I want different...
4
by: Broeisi | last post by:
Hello, I'm trying to write a console based program in C in Linux. I want to use the function keys in my program, but I don;t know how to let the C program know when for exmaple the F1 key is...
14
by: vatamane | last post by:
This has been bothering me for a while. Just want to find out if it just me or perhaps others have thought of this too: Why shouldn't the keyset of a dictionary be represented as a set instead of a...
1
by: asger_gronnow | last post by:
Hi I'm having trouble associating file types with my app. Associating a file type works as long as the file type is already associated with another program but if the file type doesn't exist...
3
by: bg_ie | last post by:
Hi, Lets say I have a TreeView with nodes that represent objects of a number of different types. Is there a way of associating the Set method of each object with its node in the TreeView? That...
2
by: Charles Law | last post by:
I'll kick myself when you tell me, but ... I have a user control on a form, and I want the user control to see the arrow keys when I press them. If I press just about any other key the control's...
1
by: Sam Samson | last post by:
Greeetings All .. For my users convenience I have mapped function keys F2 .. F12 to change the tabs on the Tabcontrol. Works like a charm <ominous musicuntil one hits F8</ominous music> ...
9
by: Wingot | last post by:
Hey, I am using Visual Studio 2008 Beta 2 for some application development in C#, but I presume that the following question applies equally well to any environment.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.