Linux C 实现 键盘全局监听 覆盖剪切板功能

在开发的过程中,常常重复输入一些东西,感觉效率较低,如我常使用的scp命令,里面常常需要输入ip地址,每次都重复的键入比较烦。所以准备自己写一个剪切板。功能,按下快捷键,我常用的一些重复命令,自动复制进剪切板中。

测试环境

Ubuntu14.04/Ubuntu16.04

功能简介

首先键盘监听,直接对/dev/input/eventX进行全局监听(需要root权限,)。
当指定的快捷键按下后,将我预设的字符串拷贝进剪切板,以供我粘贴使用。

 

源码

keyboard_watch.c

注意: find_event函数帮助你找到键盘event号,如果不准,请手动测试以及设置event号。

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <sys/mman.h>

#include <linux/input.h>
#include <fcntl.h>

#define DBG_PRINTF(...)  
//#define DBG_PRINTF printf
 
int find_event(char *sub_buff)
{
	int iFd;
        FILE *tFp;
	struct stat tStat;
	char *command="cat /proc/bus/input/devices > log.txt" ;
	char *file_path="./log.txt";
 	int number;
	unsigned char  *file_buff;
	/* according  to mouse name find event number*/
	
	char *buff;
 
	tFp=fopen(file_path,"r+");    /* check if have log.txt file */
	
	if(NULL!=tFp) {
	  fclose(tFp);
	  system("rm log.txt");
	}
 
	system(command);
	/* 打开文件 */
	tFp = fopen(file_path, "r+");
	
	if (tFp == NULL) {
		DBG_PRINTF("can't open %s\n", file_path);
		return -1;
	}
	
        iFd = fileno(tFp);
 
	fstat(iFd, &tStat);
	 /* mmap the file to mem */
	file_buff = (unsigned char *)mmap(NULL , tStat.st_size, PROT_READ |         PROT_WRITE, MAP_SHARED, iFd, 0);

	if (file_buff == (unsigned char *)-1) {
		DBG_PRINTF("mmap error!\n");
		return -1;
	}

	buff = strstr(file_buff,sub_buff);/* in the mem file_buff  find sub_buff name */
	
	if(NULL == buff) {
   		DBG_PRINTF("can't find %s\n",sub_buff);
   		munmap(file_buff, tStat.st_size);
		return -1;
	}

        number = *(buff+strlen(sub_buff)+6);/* 6== event */
	munmap(file_buff, tStat.st_size);
	fclose(tFp);
	return  number;
}


int main(int argc, char **argv)
{
	int number;
	char *keyword="Handlers=sysrq kbd";
	int keys_fd;
	struct input_event t;
	char dev_path[20];
	unsigned char ctrl_down_flag = 0, state = 0;
	unsigned func_enable = 1;

	number = find_event(keyword);
	DBG_PRINTF("found keyboard device:/dev/input/event%d\n",number-'0');
	
	sprintf(dev_path,"/dev/input/event%d",number-'0');
	DBG_PRINTF("%s",dev_path);
	keys_fd = open(dev_path, O_RDONLY);
	if(keys_fd <= 0) {
		printf("open /dev/input/event%d device error!\n",number - '0');
		return -1;
	}

	while(1) {
		if(read(keys_fd, &t, sizeof(t)) == sizeof(t)) {
			if(t.type == EV_KEY)
				if(t.value== 0 || t.value ==1 ) {
					DBG_PRINTF("key %d %s\n", t.code, (t.value) ? "Pressed" : "Released");

					if(t.code == 97 && t.value == 1) {
						ctrl_down_flag = 1;
						state = 1;
					} else if(state == 1) {
						state = 2;
					} else {
						ctrl_down_flag = 0;
						state = 0;
					}
					
					if(t.code == 102 && ctrl_down_flag)
						break;

					if(t.code == 102 && t.value == 0) {
						func_enable ^= 1;
						DBG_PRINTF("func_enable=%d \n",func_enable);
					}
					
					if(func_enable) {
						if(t.code == 70 && t.value == 1) {
							DBG_PRINTF("scp xxxx@192.168.x.x:/data/ \n");
							system("echo \"scp xxxx@192.168.x.x:/data/ \\c\" | xsel --clipboard");
						}

						if(t.code == 119 && t.value == 1) {
							DBG_PRINTF("xxxx@192.168.x.x\n");
							system("echo \"xxxx@192.168.x.x\\c\" | xsel --clipboard");
						}
					}
			}
		}
	}	
	close(keys_fd);
	return 0;
}

 

预安装

剪切板复制和粘贴功能依赖xsel命令,故先安装之。

sudo apt-get install xsel

 

编译执行

gcc keyboard_watch.c -o  keyboard_watch
sudo ./keyboard_watch

 

使用简介

键盘快捷键逻辑可以自行更改,这里默认为:


按下home键:临时打开/关闭功能(默认为打开)
按下右ctrl+home键:彻底退出程序
按下scroll键:将预设的字符串1拷贝进剪切板
按下pause键:将预设的字符串2拷贝进剪切板

 

参考博客

https://blog.csdn.net/qq_21792169/article/details/51458855

https://blog.csdn.net/special00/article/details/82355556

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值