Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 20th, 2005, 03:26 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default Capturing the onkeydown event

I'm writing a corporate intranet-based CMS. I'm trying to capture the onkeydown event to create keyboard shortcuts. Below I'm adding the "ctrl + s" keyboard shortcut to save information.
Code:
function register_hotkeys(e)
{
    //alert(e.keyCode +  " : " + String.fromCharCode(e.keyCode));
    if (e.ctrlKey && e.keyCode == 83) // 83 == s, see if ctrl + s is being pressed
    {
        document.getElementById('control-panel-form').submit();
    }
    return false;
}

window.onkeydown = register_hotkeys;
I've googled extensively on creating keyboard shortcuts, but can't seem to find anything that discusses overriding default, built-in shortcuts, such as ctrl+s, as is the case in the example here. When I run this the form submits, but the default "save page as" dialog of Firefox still fires off.

This app is Gecko-only, so I'm looking for a solution that will be compatible with Mozilla, et all. Not too interested in Explorer, but I wouldn't mind hearing about a method that works in Explorer.

Is there a solution, or is this a fruitless quest?

Regards,
Rich

--
[http://www.smilingsouls.net]
Mail_IMAP: A PHP/C-Client/PEAR solution for webmail
Author: Beginning CSS: Cascading Style Sheets For Web Design
Reply With Quote
  #2 (permalink)  
Old May 27th, 2005, 03:13 PM
nzakas's Avatar
Wrox Author
 
Join Date: Dec 2004
Location: Peabody, MA, USA.
Posts: 217
Thanks: 0
Thanked 5 Times in 5 Posts
Default

I'm not sure if it's possible, but if it is, you'd need to prevent the default action of the event:

Code:
function register_hotkeys(e)
{
    //alert(e.keyCode +  " : " + String.fromCharCode(e.keyCode));
    if (e.ctrlKey && e.keyCode == 83) // 83 == s, see if ctrl + s is being pressed
    {
        document.getElementById('control-panel-form').submit();
    }
    //return false;
    e.preventDefault();
}
Nicholas C. Zakas
Author, Professional JavaScript for Web Developers (ISBN 0764579088)
http://www.nczonline.net/
Reply With Quote
  #3 (permalink)  
Old May 31st, 2005, 03:35 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Thanks for your input Nicholas, I'll try it out and report the results.

Regards,
Rich

--
[http://www.smilingsouls.net]
Mail_IMAP: A PHP/C-Client/PEAR solution for webmail
Author: Beginning CSS: Cascading Style Sheets For Web Design
Reply With Quote
  #4 (permalink)  
Old June 3rd, 2005, 10:39 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I actually tried a number of options but none worked. If you test event.isCancelable you'll see that it's not so preventDefault doesn't work. You can gget your function to fire but the default save still appears.

--

Joe (Microsoft MVP - XML)
Reply With Quote
  #5 (permalink)  
Old June 3rd, 2005, 11:21 AM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Thanks Joe, I haven't been able to come back around to this application for a while, so I haven't tried anything else yet. I might try posing my question on one of the Mozilla newsgroups. Perhaps there's some XUL voodoo I can invoke.

If I discover anything new I'll post my findings.

Regards,
Rich

--
[http://www.smilingsouls.net]
Mail_IMAP: A PHP/C-Client/PEAR solution for webmail
Author: Beginning CSS: Cascading Style Sheets For Web Design
Reply With Quote
  #6 (permalink)  
Old December 27th, 2005, 04:27 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

UPDATE: e.preventDefault(); works to cancel the appearance of the save as dialogue in Firefox 1.5. This also cancels the default event for any key press, including typing text into a text box. So, the best way to incorporate it is as follows.

Code:
function register_hotkeys(e)
{
    if (e.ctrlKey && e.keyCode == 83) // 83 == s, see if ctrl + s is being pressed
    {
        document.getElementById('control-panel-form').submit();
        e.preventDefault();  // No "save page as" dialogue!
    }
}
Regards,
Rich

--
[http://www.smilingsouls.net]
Mail_IMAP: A PHP/C-Client/PEAR solution for webmail
Author: Beginning CSS: Cascading Style Sheets For Web Design
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
mouse event capturing in IE6 using VB6 amyourpet VB How-To 0 January 25th, 2008 08:01 AM
Capturing a "Enter" key event in C# windows applic bhavna VS.NET 2002/2003 1 March 15th, 2007 07:45 AM
Capturing Mouse Click Event whiterainbow ASP.NET 1.0 and 1.1 Professional 1 December 8th, 2005 01:44 AM
Capturing the Control + C event Sammy8932 Access VBA 8 June 8th, 2005 09:05 AM



All times are GMT -4. The time now is 06:36 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.