Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 20th, 2005, 03:26 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
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
 
Old May 27th, 2005, 03:13 PM
nzakas's Avatar
Wrox Author
 
Join Date: Dec 2004
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/
 
Old May 31st, 2005, 03:35 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
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
 
Old June 3rd, 2005, 10:39 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
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)
 
Old June 3rd, 2005, 11:21 AM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
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
 
Old December 27th, 2005, 04:27 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
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





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.