Build a Linux Clipboard Translator with C, xclip, and translate-shell
This guide walks through creating a Linux word‑and‑phrase translation tool that captures selected text via the X clipboard, translates it with translate‑shell, and displays the result in the terminal, using only a few command‑line utilities and a small C program.
Overview
This guide shows how to build a lightweight Linux word‑translation tool that detects a mouse left‑button release, reads the selected text from the X clipboard, translates it with translate-shell, and prints the result in the terminal.
1. Install required commands
Install xclip to access the X clipboard: $ sudo apt install xclip Common usages:
$ xclip file_name # copy file content to X clipboard
$ xclip -selection c file_name # copy to the external clipboard
$ xclip -o # output X clipboard to terminal
$ xclip -selection c -o # output external clipboard to terminalInstall translate-shell , a CLI front‑end for Google/Bing translation: $ sudo apt install translate-shell Basic commands:
$ trans en:zh [word] # translate a single word
$ trans en:zh -b [text] # brief translation of a short textInternet access is required unless the default engine is changed.
2. Locate the mouse device
Mouse events are exposed under /proc/bus/input/devices. Run: $ sudo cat /proc/bus/input/devices Find the line Handlers=... eventX (e.g., event2). Verify the correct event file by monitoring its output: $ sudo cat /dev/input/event2 | hexdump If moving or clicking the mouse produces values, that eventX corresponds to your mouse.
3. Write the C program
Create a directory ~/Translator and add ct.c with the following source:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/input.h>
#include <fcntl.h>
int main(void) {
int fd;
struct input_event ev;
// Adjust the device path if needed
fd = open("/dev/input/event2", O_RDONLY);
if (fd <= 0) {
printf("open /dev/input/event2 error!
");
return -1;
}
while (1) {
read(fd, &ev, sizeof(ev));
if (ev.type == EV_KEY && ev.code == BTN_LEFT && ev.value == MSC_SERIAL) {
// Left button released – invoke translation script
system("~/Translator/goTranslate.sh");
}
}
close(fd);
return 0;
}Compile the program:
$ gcc ct.c -o ct4. Create the translation shell script
Save the following as ~/Translator/goTranslate.sh and make it executable ( chmod +x):
#!/bin/bash
str_old=$(cat ~/Translator/lastContent 2>/dev/null || echo "")
str_new=$(xclip -o 2>/dev/null | xargs)
if [[ "$str_new" != "$str_old" && -n "$str_new" ]]; then
echo -e "
"
count=$(echo "$str_new" | wc -w)
if [[ "$count" == "1" ]]; then
echo -n -e "$str_new " >>~/Translator/words
echo "$str_new" | trans :zh-CN | tail -1 | cut -c5- |
sed "s,\x1b\[[0-9;]*[a-zA-Z],,g" | tee -a ~/Translator/words
else
echo "$str_new" | trans :zh-CN -b
fi
echo "$str_new" >~/Translator/lastContent
fiCreate an empty cache file:
$ touch ~/Translator/lastContent5. Set up a convenient alias
Add the following line to ~/.bashrc (or the appropriate shell startup file): alias ct='sudo ~/Translator/ct' Reload the shell ( source ~/.bashrc) or open a new terminal.
6. Usage
When reading a document, select English text with the mouse. Then run the alias: $ ct The C program detects the left‑button release, the script fetches the selected text via xclip, translates it with trans, and prints the Chinese result. Keeping the terminal window small and on top prevents it from obscuring the document.
Conclusion
The solution combines standard Linux utilities ( xclip, translate-shell) with a short C program that monitors /dev/input/eventX. It provides on‑the‑fly English‑to‑Chinese translation of selected words or phrases without leaving the terminal.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
