Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Keyboard and Mouse Events | Events and Event Handling
Advanced JavaScript Mastery
course content

Conteúdo do Curso

Advanced JavaScript Mastery

Advanced JavaScript Mastery

1. Classes
2. DOM Manipulation
3. Events and Event Handling
4. Asynchronous JavaScript and APIs

bookKeyboard and Mouse Events

Keyboard and mouse events allow developers to capture and respond to user interactions. By handling user inputs effectively, you can provide feedback, execute specific actions, or enhance the overall user experience.

Keyboard Events

Keyboard events are triggered when users interact with the keyboard. There are two primary keyboard events:

In a text editor or any form where users type, it's common to capture certain key combinations (e.g., Ctrl + S to save, or Ctrl + Z to undo). Let's create an example where specific keys are logged to simulate shortcut detection in a form.

html

index

css

index

js

index

copy

In this example, users type into an input field, and the script captures common keyboard shortcuts like Ctrl + S for saving or Ctrl + Z for undoing an action.

Handling Mouse Events

Mouse events are triggered when users interact with their mouse or a touchpad. The most commonly used mouse events are:

Let's create an example where a tooltip is displayed when the mouse hovers over a button.

html

index

css

index

js

index

copy

Offering quick, contextual information without cluttering the interface is a common use case, improving usability and guiding users through complex interfaces.

Practical Example: Double-Click to Edit with Save Action

In many modern web applications, users can double-click to edit an item (like renaming a file or updating a task) and save their changes.

html

index

css

index

js

index

copy

In this example, Initially, a paragraph displays the task, which becomes editable when double-clicked, showing an input field and a "Save" button. The user can modify the task and click "Save" to apply changes; this setup avoids accidental changes from single clicks. After saving, a status message appears to confirm the update or display an error if the input is empty.

1. What does the `keydown` event do?
2. Which mouse event is triggered when the pointer enters an element's area?
What does the `keydown` event do?

What does the keydown event do?

Selecione a resposta correta

Which mouse event is triggered when the pointer enters an element's area?

Which mouse event is triggered when the pointer enters an element's area?

Selecione a resposta correta

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 6
We're sorry to hear that something went wrong. What happened?
some-alt