Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
PHP jQuery Cookbook

You're reading from   PHP jQuery Cookbook jQuery and PHP are the dynamic duo that will allow you to build powerful web applications. This Cookbook is the easy way in with over 60 recipes covering everything from the basics to creating plugins and integrating databases.

Arrow left icon
Product type Paperback
Published in Dec 2010
Publisher Packt
ISBN-13 9781849512749
Length 332 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Vijay Joshi Vijay Joshi
Author Profile Icon Vijay Joshi
Vijay Joshi
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

PHP jQuery Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Handling Events with jQuery FREE CHAPTER 2. Combining PHP and jQuery 3. Working with XML Documents 4. Working with JSON 5. Working with Forms 6. Adding Visual Effects to Forms 7. Creating Cool Navigation Menus 8. Data Binding with PHP and jQuery 9. Enhancing your Site with PHP and jQuery Firebug Index

Creating keyboard shortcuts


Keyboard navigation is common in window-based applications. This is very handy for those who prefer keyboard controls over mouse controls. Keyboard shortcuts can also be created in web applications but they are difficult to implement due to inconsistency among browsers.

We will create a simple example in this recipe that will give you the basic understanding of implementing shortcut keys. You will be able to create your own shortcut keys for use in your web applications.

Getting ready

Get the jQuery library to use with this recipe.

How to do it...

  1. Create a new file named keyboard.html and save it in the chapter1 directory.

  2. In the body of HTML create two DIV elements and in the <head> section write some CSS to apply styles to these DIV elements.

    <html>
      <head>
        <title>Keyboard Shortcuts</title>
        <style type="text/css">
        div{ border : 1px solid black;float:left;height:200px; margin:10px; width:220px;}
        </style>
      </head>
      <body>
        <div>You can toggle this div using Alt+S</div>
        
        <div>You can toggle this div using Alt+G </div>
        
        <p style="clear:both;">&nbsp;</p>
        <p>Press Alt+B to toggle both divs</p>
      </body>
    </html>
  3. Write the jQuery code that will create keyboard shortcuts to toggle these DIV elements. The keydown event handler will be used to implement this behaviour. It will check for the keys that are pressed and then take actions accordingly. Three shortcuts will be created. Pressing Alt + S will toggle the first DIV. Alt + G will toggle the second DIV. Pressing Alt + B will toggle both the DIV elements together.

    Another handler keyup will be used to reset the required variables.

    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
      $(document).ready(function ()
      {
        //remember that this is a global variable
        var altPressed = false;
        $(document).keydown(function (event) 
        {
          if(event.which == 18) 
            altPressed = true;
          if(altPressed)
          {
             switch(event.which)
            {
               case 83:
                   $('div:first').slideToggle('slow');
                   return false;
                   break;
               case 71:
                   $('div:last').slideToggle('slow');
                   return false;
                   break;
               case 66:
                   $('div').slideToggle('slow');
                   return false;
                   break;
            }
          }
        });
    
        $(document).keyup(function (event) {
          if(event.which == 18) 
            altPressed = false;
        });
      });
    </script>
  4. Open your browser and run the keyboard.html file. Try pressing the shortcuts that we have just created. You will see that the DIV elements will toggle with a slide effect.

How it works...

In order to be able to create shortcut keys, first we need to find out which key was pressed. Different browsers have their own methods of determining the value of the pressed key. jQuery normalizes the way this information can be retrieved across browsers. An event object is available to handler functions. This event object has a property which that gives the code of the pressed key. Alt key has the value 18.

The keyboard shortcuts in this recipe use the combination of Alt and the other keys. We begin by declaring a global variable altPressed with the value set to false. Then there are two events attached to the page. keydown will execute when a key is in a pressed state and keyup when a key is released. Whenever Alt is pressed the keydown event will set its value to true. When released, it will be reset to false again by the keyup handler function.

Next comes the if statement, which will evaluate to a true value if the Alt key is pressed. If Alt is pressed and another key is pressed along with it, the switch case will check the key's value and will execute the corresponding switch case.

The value for the S key is 83. So, pressing S along with Alt will select the first DIV and will apply the slideToggle effect to it. Similarly, Alt + G will toggle the second DIV and Alt + B will toggle both DIVs.

Note

Note the return of false in each case of switch statement. Returning false is necessary to override a browser's default behavior. If false is not returned, pressing the Alt key will activate the browser's menu.

There's more...

List of common key codes

A list of key codes can be found at http://goo.gl/v2Fk

See also

  • Binding and unbinding elements in this chapter explains how to attach events to elements.

You have been reading a chapter from
PHP jQuery Cookbook
Published in: Dec 2010
Publisher: Packt
ISBN-13: 9781849512749
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image