showkey Command in Linux

Introduction

In this hands-on lab, we'll delve into the Linux showkey command, exploring its functionality and practical applications for systemadmin tasks. The showkey command serves as a valuable utility, enabling you to capture and display the key codes generated by your keyboard. This capability proves invaluable when troubleshooting keyboard-related problems or gaining insights into how your keyboard interacts with the underlying system. Our focus will be on leveraging showkey to capture keyboard input and thoroughly analyze the resulting output. By the end of this lab, you'll have a firm understanding of the showkey command and its diverse use cases in a Linux environment.

Understand the Purpose of the showkey Command

This section will focus on clarifying the specific purpose of the showkey command within Linux. As a systemadmin, understanding the utilities at your disposal is critical, and showkey is a prime example. It captures and displays the key codes generated by your keyboard when a key is pressed. This can be extremely beneficial for identifying hardware or software related issues when troubleshooting, or gaining insights into how your keyboard communicates at the system level.

To demonstrate its use, start by running showkey in your terminal:

sudo showkey

Example output:

Press any keys - Ctrl-C to end
key pressed 1 is 65 (a)
key pressed 2 is 66 (b)
key pressed 3 is 67 (c)
^C

The showkey command remains active, dynamically displaying the key codes for each key you press. This real-time feedback reveals the decimal code associated with each key, alongside the corresponding character (when applicable) enclosed in parentheses. As a root user or admin, this command will help in understanding what the system is receiving as input.

Here are a few specific ways a systemadmin can leverage the showkey command:

  1. Keyboard Troubleshooting: When diagnosing keyboard malfunctions (e.g., unresponsive keys or unexpected behavior), showkey assists in pinpointing the underlying cause by presenting the generated key codes. This enables you to isolate hardware issues from software misconfigurations.

  2. Custom Keyboard Shortcut Configuration: For customizing keyboard shortcuts or macros, showkey reveals the precise key codes required in your configuration. This helps in accurately mapping functions to specific key combinations.

  3. Keyboard-Driven Application Debugging: Software developers creating applications that interact directly with keyboard input use showkey to decipher the key codes that their program is receiving. This is crucial for debugging input handling routines.

To terminate the showkey command, press Ctrl+C.

Capture Keyboard Input Using the showkey Command

In this section, we will demonstrate capturing keyboard input using the showkey command.

Initiate the showkey command in your terminal:

sudo showkey

This places showkey into a listening mode, actively awaiting key presses on your keyboard.

Now, press various keys on your keyboard. Observe the key codes being displayed:

Press any keys - Ctrl-C to end
key pressed 1 is 65 (a)
key pressed 2 is 66 (b)
key pressed 3 is 67 (c)

The output will show the decimal code corresponding to each key, along with the character representation in parentheses.

Experiment with special keys like arrow keys, function keys (F1-F12), and modifier keys (Shift, Ctrl, Alt). showkey will reveal the key codes associated with these as well.

To exit showkey, press Ctrl+C.

Analyze the Output of the showkey Command

In this section, we'll dissect the showkey command's output to understand the meaning and interpretation of the presented key codes.

Relaunch the showkey command:

sudo showkey

This time, press different keys – letters, numbers, function keys, and special characters. Examine the output:

Press any keys - Ctrl-C to end
key pressed 1 is 97 (a)
key pressed 2 is 98 (b)
key pressed 3 is 99 (c)
key pressed 4 is 49 (1)
key pressed 5 is 50 (2)
key pressed 6 is 51 (3)
key pressed 7 is 9 (Tab)
key pressed 8 is 10 (Enter)
key pressed 9 is 27 (Esc)
key pressed 10 is 32 (Space)
key pressed 11 is 113 (q)
key pressed 12 is 119 (w)
key pressed 13 is 101 (e)

The output displays the decimal code for each key and the corresponding character. Here's a breakdown:

  • "key pressed" indicates keyboard input has occurred.
  • The decimal code is the scancode, tied to the specific keyboard hardware. These codes are keyboard-specific and can vary based on layout and manufacturer.
  • The character in parentheses is the ASCII or Unicode representation of the key code; this is what the system interprets and processes.

As a systemadmin, here is how showkey's output can be helpful:

  1. Keyboard Issue Resolution: Troubleshooting keyboard problems (e.g., non-responsive or erratic keys)? showkey exposes key codes to uncover the source of the problem.
  2. Custom Shortcut Creation: Defining custom keyboard shortcuts? Use showkey to discover the key codes for your configuration.
  3. Keyboard-Aware Application Debugging: Building applications interacting with keyboard input? showkey decodes the key codes your application receives.

Exit showkey by pressing Ctrl+C.

Summary

This lab provided a comprehensive exploration of the Linux showkey command, focusing on its purpose and practical usage. We discovered that showkey allows capturing and displaying the key codes generated by a keyboard. This capability proves invaluable in various scenarios, including troubleshooting keyboard-related issues, facilitating custom keyboard shortcut mapping, and aiding in debugging keyboard-sensitive applications. We initiated the showkey command within the terminal, carefully observed its output, and noted the decimal codes associated with each key press, along with their corresponding character representations. Gaining this knowledge empowers systemadmin to better understand the keyboard's communication with the Linux system.

400+ Linux Commands