Keyboard Event with jQuery

Keyboard entry is also an Event. Learn in this tutorial, how you can enhance your user interface when a user of your website hits certain keys on their keyboard

PAIDLevel: Intermediate7:13 mins
Keyboard Event with jQuery
You do not have access to this lesson!

Enroll in the training to get access to this lesson along with hundreds of other lessons and complete courses.

Already enrolled? Login

Course content
Lessons #1: Getting Started with jQuery PAID
10:21 mins
Lessons #2: Find Something / Query the DOM with jQuery - part 1 PAID
12:52 mins
Lessons #3: Find Something / Query the DOM with jQuery - part 2 PAID
22:25 mins
Lessons #4: Find Something / Query the DOM with jQuery - part 3 PAID
14:24 mins
Lessons #5: Do Something with jQuery PAID
11:39 mins
Lessons #6: Events with jQuery PAID
19:47 mins
Lessons #7: Project: Create a Top Bar Widget with jQuery PAID
23:47 mins
Lessons #8: Project: Create Tabs functionality with jQuery PAID
10:46 mins
Lessons #9: Animate: Animating elements with jQuery PAID
9:41 mins
Lessons #10: Event Delegation with jQuery PAID
4:52 mins
Lessons #11: Event Propagation with jQuery PAID
6:20 mins
Lessons #12: Prevent Default Behavior (of elements) with jQuery PAID
3:06 mins
Lessons #13: Content Manipulation with jQuery PAID
9:32 mins
Lessons #14: Keyboard Event with jQuery PAID
7:13 mins
Lessons #15: Project: Create your own Image Carousel Slider with jQuery PAID
27:07 mins
Lessons #16: Ajax: Create a Single Page App with jQuery PAID
15:09 mins
Lessons #17: Ajax: Create a Location Finder App using jQuery & Google Maps API - Part 1 PAID
9:53 mins
Lessons #18: Ajax: Create a Location Finder App using jQuery & Google Maps API - Part 2 PAID
4:18 mins

Keyboard Event with jQuery

In this lesson, we are going to learn how to create keyboard shortcuts in our web application In the image below you see the list of shortcuts that Gmail has.



Our Document Structure

In our document I have some basic HTML page created like such 


It has some styles as well


In the script.js file I have written some jQuery so when the "click me" button is clicked, the text changes to "I have been clicked"


In this lesson, we are going to learn three keyboard events

  • Keypress
  • Keyup
  • Keydown

I'm going to query the DOM and write some jQuery by writing keypress and also put e which is the event call back. For demonstration purposes I'll console.log('I have been pressed', e);

$(document).ready(function($){

  $('button').on('click', function() {
    $(this).text('I have been clicked');
})
  $('body').on('keypress', function (e) {
   console.log('I have been pressed', e);
})

});

Save it refresh the page and our page looks like this
 


I'm going to open my console and now if I hit A while on the page, you will see the event logged in the console. If I open the object in the console, you will see lots of properties. The keyCode: 97 represents the A key that was pressed. The keyCode is the key code representation of a lower case a



There's a site out there for JavaScript Char Codes which gives you the quick conversion of all the keys that a keyboard has and it's character code. 


So in our code we can recognize what kind of key was pressed and then take some action after that key was pressed. There are three kinds of keyboard events. When you press down, it's a key down. When you press up, it's a key up. Just between the key up and key down event, the key pressed event triggers. 

I'm going to create a keyboard shortcut that when when Ctrl + J is pressed the "click me" button on our webpage is going to be triggered. I'll use the keydown in our example. I'm going to write jQuery like such
e.keyCode === 74 && e.ctrlKey === true what it means is that when the J is clicked while holding the shift key, the button is going to be clicked by writing $('button').click(); as shown below

$(document).ready(function($){

  $('button').on('click', function() {
    $(this).text('I have been clicked');
})

  $('body').on('keypress', function (e) { if(e.keyCode === 74 && e.ctrlKey === true $('button').click();
   console.log('I have been keydown', e);
})

});

Save it, refresh the screen and you see when I hit Ctrl + J the button got clicked


We can also modify a little bit as well like for example if you don't want it to work only when Ctrl + J is clicked but also when command + J is clicked or maybe C is clicked. So I'm going to write jQuery as shown below, as well as in the screenshot

$(document).ready(function($){

  $('button').on('click', function() {
    $(this).text('I have been clicked');
})

  $('body').on('keypress', function (e) { if(e.ctrlKey === true){ if(e.keyCode === 74 || e.keyCode === 67){ $('button').click();
       console.log('I have been keydown', e);
})

});
Save it, refresh the screen and now if I click Ctrl + C or Ctrl + J you will see the triggered happened.



I hope you enjoyed this lesson, if you have any question, leave your comments below. I'll talk to you in the next lesson. Good bye :) 
Become a confident software developer with mentor support!
Get Started
Already have an account? Please Login