473,657 Members | 2,604 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1479
On 23 Jul 2005 17:39:25 -0700, in comp.lang.javas cript
fr**********@us a.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******@yourpa ntsyahoo.com.au : Remove your pants to reply
---------------------------------------------------------------
Jul 24 '05 #2
fr**********@us a.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.onkeyp ress=function(e vt){
evt=evt||window .event;

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

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

var key=String.from CharCode(k).toL owerCase();
for(var ii=0, m; ii<macros.lengt h; ii++) {
m=macros[ii];
if(m.key==key && m.modifiers==ha sCtrl){
m.func(evt);
}
}
if(window.focus ) window.focus();
}

return function(func, key, modifiers) {
macros[macros.length]={
func:func,
key:key.toLower Case(),
modifiers:modif iers||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
1864
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 name. The invoices pertaining to the customers is displayed with the check box. Once the user checks the particular check box the status of the particular check box ie. the particular invoice field should be updated to PAID. I got stuck up in...
2
3449
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 sections of the webpage to be coloured differently depending on variables in my code and I use CSS classes to achieve this.
4
6682
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 pressed. I want to be able to use all 12 Function keys in my program. Can someone help me maybe on this one? I guess that those function keys also have an ascii number.
14
3452
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 list? I know that sets were introduced a lot later and lists/dictionaries were used instead but I think "the only correct way" now is for the dictionary keys and values to be sets. Presently {1:0,2:0,3:0}.keys() will produce but it could also...
1
1723
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 already, it isn't associated. So my question is: how do I associate a file type that isn't registered already? How can I register this new file type if that's what I'll have to do before associating it. I'm programming in Win32 using the Dev-Cpp...
3
1718
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 way when you select a node in order to change its associated data, you have easy access to its Set function. The way I normally handel this situation is is to create an enum of all the different types that the treeview contains. I then have a
2
3313
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 KeyDown event is fired, but not when I press a direction key. I want to see them in the KeyDown event so that I can respond as soon as the key is depressed, and I want to detect when the key is held down so that I can perform an action...
1
2046
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> That particular tabpage has a webbrowser object embedded in it .. and after it flicks to that tabpage it no longer plays nice with the other Tabpages .. click on another Tab using the mouse and all is wonderful again. (unless
9
2391
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
8385
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8303
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8821
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8723
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8502
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7316
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1601
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.