15 October 2025

Creating Reliable Keyboard Shortcuts for Copy/Paste and Middle-Click


# Creating Reliable Keyboard Shortcuts for Copy/Paste and Middle-Click

## Problem: The Need for Alternative Shortcuts

I found that relying solely on the mouse for basic text operations was limiting:

1.  My **mouse scroll wheel button (Middle-click)**, often used for quickly pasting selected text, was becoming **unreliable**.
2.  I frequently **dislike moving my right hand** from the arrow/navigation keys and Enter area to reach $\text{Ctrl}$+$\text{C}$ or $\text{Ctrl}$+$\text{V}$.

This setup requires a more reliable and ergonomic alternative that keeps my right hand near the arrow keys and the left one on the mouse.

### Context

This solution was developed and tested on **Debian 10 (Buster)** using the **LXDE desktop environment** with the **Openbox Window Manager**.

-----

## Solution: Mapping to the Super Key and Arrow Keys

The solution is to create new keybindings using the **Right Super Key ($\text{Super\_R}$)**—a key often conveniently located near the right-hand side of the keyboard—combined with the navigation keys.

The actions are mapped as follows:

| Key Combination | Action | Description |
| :--- | :--- | :--- |
| **Super + Left** | Copy ($\text{Ctrl}$+$\text{C}$) | Copies the currently selected text to the standard clipboard. |
| **Super + Right** | Paste ($\text{Ctrl}$+$\text{V}$) | Pastes the contents of the standard clipboard. |
| **Super + Up** | Paste Primary Selection | **Simulates the middle mouse click**, pasting the text that was highlighted (Primary Selection). |

### The Critical Fix: Preventing "Stuck" Keys

A common issue when binding system keys (like $\text{Super}$) to execute external scripts in Openbox is that the $\text{Super}$ key can get "stuck" in a pressed state after the script finishes. This is because the $\text{Key Release}$ event is sometimes missed.

The fix is built directly into the script using the `xdotool keyup` command to explicitly release all modifier keys.

-----

## Actions: Implementation Steps

### 1\. Prerequisite Tools

Ensure the necessary command-line utilities for clipboard management and input simulation are installed:

```bash
sudo apt update
sudo apt install xdotool xclip
```

### 2\. Create the Consolidated Bash Script

This single script, which we'll call `shortcut-handler.sh`, handles all three copy/paste actions based on a single argument. It also includes the crucial logic to clear stuck keys.

**File: `/path/to/shortcut-handler.sh`**

```bash
#!/bin/bash

# --- 1. Input Validation ---
ACTION="$1"
if [ -z "$ACTION" ]; then
    echo "Error: No action specified." >&2
    echo "Usage: $0 {copy|paste|middleclick}" >&2
    exit 1
fi

# --- 2. Pre-Execution Key Cleanup ---
# CRITICAL: Virtually release all common modifier keys before proceeding.
# This prevents the next action from failing if a modifier is stuck.
xdotool keyup Super_L Super_R Control_L Control_R Alt_L Alt_R Shift_L Shift_R

# --- 3. Execute Action ---
case "$ACTION" in
    # Simulates standard Ctrl+V (Clipboard Selection)
    paste)
        xdotool key --clearmodifiers Control+v
        ;;

    # Simulates standard Ctrl+C (Clipboard Selection)
    copy)
        xdotool key --clearmodifiers Control+c
        ;;

    # Pastes the Primary Selection (highlighted text), which is more reliable than a simple 'click 2'
    middleclick)
        xclip -out -selection primary | xdotool type --clearmodifiers --delay 1 --file -
        ;;

    # Handle invalid arguments
    *)
        echo "Error: Invalid action '$ACTION'. Use: copy, paste, or middleclick." >&2
        exit 1
        ;;
esac

# --- 4. Post-Execution Key Cleanup ---
# Explicitly release the Super key again to guarantee it's not stuck after execution.
xdotool keyup Super_L keyup Super_R

exit 0
```

**Make the script executable:**

```bash
chmod +x /path/to/shortcut-handler.sh
```

### 3\. Configure Openbox Keybindings

Edit your Openbox configuration file, typically located at `~/.config/openbox/rc.xml`. Add the following keybinds within the `<keyboard>` tags.

**Note:** Openbox uses the letter **`W`** to represent the $\text{Super}$ (Windows) key.

```xml
    <keybind key="W-Left">
      <action name="Execute">
        <command>sh -c '/path/to/shortcut-handler.sh copy'</command>
      </action>
    </keybind>

    <keybind key="W-Right">
      <action name="Execute">
        <command>sh -c '/path/to/shortcut-handler.sh paste'</command>
      </action>
    </keybind>

    <keybind key="W-Up">
      <action name="Execute">
        <command>sh -c '/path/to/shortcut-handler.sh middleclick'</command>
      </action>
    </keybind>
```

### 4\. Reload Openbox

Save the `rc.xml` file and run the following command in a terminal to apply the new shortcuts:

```bash
openbox --reconfigure
```

-----

## Key Takeaways

The development process yielded several important lessons for reliable Openbox keybinding configuration:

  * **Backup is King:** Always back up your `~/.config/openbox/rc.xml` before making changes.
  * **The Shell Wrapper is Essential:** The `<command>` tag in `rc.xml` only accepts a single simple command. To chain commands or pass arguments, you must wrap the entire sequence in an explicit shell call: `sh -c 'command1; command2'`.
  * **Preventing Stuck Keys:** The `xdotool keyup Super_L keyup Super_R` command is the reliable remedy for the $\text{Super}$ key getting stuck after executing a script. Placing it *inside* the script, alongside a pre-execution cleanup, makes the overall solution robust.
  * **Debugging Scripts:** When testing, use simple logging inside the script to confirm it's being executed and to check for errors:
    ```bash
    # Add to the script for debugging (DISABLE WHEN DONE)
    echo "$(date) - Running $ACTION" >> /tmp/openbox-debug.log
    ```
Too Cool for Internet Explorer