📄 kbde.c
字号:
/* keyboard emulator project Copyright (C) 1998-2003 Reznic Valery <valery_reznic@users.sourceforge.net> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. */#include <stdio.h>#include <string.h>#include <unistd.h>#include <stdlib.h>#include <errno.h>#include <kbde.h>static const char *pgm_name = "";typedef enum { type_ascii, type_key, type_key_press, type_key_release, type_file, type_sleep} op_type;typedef struct { op_type type; union { const char *ascii; int key_code; const char *filename; long sleep; } op; long delay;} op_desc;static void key_name_list(){ int count; const char *name; for (count = 0; count <= KBDE_KEY_LAST_USED; count++) { name = kbde_key_to_name(count); if (name) { fprintf(stdout, "%s\n", name); } }}#define DELAY_DEFAULT 300000 /* 300 msec = 0.3 sec */#define DEVICE_DEFAULT "/dev/kbde"#define MILLI_TO_MICRO 1000#define IS_STDIN(name) \ ( (strcmp(name, "-") == 0) || (strcmp(name, "/dev/stdin") == 0) )#define IS_STDOUT(name) \ ( (strcmp(name, "-") == 0) || (strcmp(name, "/dev/stdout") == 0) )#define IS_STDERR(name) \ (strcmp(name, "/dev/stderr") == 0)static void usage(){ printf( "Usage: %s [options]\n" "Options:\n" " -a, --ascii <string> - input string as sequince of ascii_keys\n" " -b, --background - run program in background\n" " -d, --delay DELAY - delay between characters's input in millisec\n" " default %d millisec\n" " -i, --input FILE - use FILE for input (for ascii_key).\n" " -k, --key key_name - key with key_name pressed and released.\n" " -l, --list - list supported key_names\n" " -o, --output FILE - use FILE for output.\n" " default - %s.\n" " -p, --press key_name - key with key_name pressed.\n" " -r, --release key_name - key with key_name released.\n" " -s, --sleep SLEEP - sleep for SLEEP millisecond\n" " -t, --text TEXT - synonim for 'ascii'.\n" " -h, --help - display this help.\n" " -v, --version - display version.\n" "\n", pgm_name, DELAY_DEFAULT / MILLI_TO_MICRO, DEVICE_DEFAULT );}static void process_input_file(const char *filename, long delay, FILE *output){ FILE *input; int need_close_input; if ( IS_STDIN(filename) ) { input = stdin; need_close_input = 0; } else { input = fopen(filename, "r"); need_close_input = 1; if (! input) { fprintf( stderr, "%s: can't open '%s' - %s\n", pgm_name, filename, strerror(errno) ); exit(1); } } while ( 1 ) { int symbol, result; char buf[10]; symbol = fgetc(input); if (symbol == EOF) break; result = kbde_ascii_key_press_release(symbol, buf); if (result > 0) { fprintf(output, "%s", buf); fflush(output); usleep(delay); } } if (need_close_input) fclose(input);}int main(int argc, char *argv[]){ long delay = DELAY_DEFAULT; char *device = DEVICE_DEFAULT; FILE *output = NULL; char buf[10]; size_t index = 0; size_t op_number = 0; op_desc operations[argc]; int need_close_output = 1; int need_fork = 0; int result; pgm_name = argv[0]; { #include "getopt.c" } if (need_fork) { result = fork(); switch(result) { case 0: /* child, continue */ break; case -1: /* error */ /* Give error message */ fprintf(stderr, "%s: can't fork - %s\n", pgm_name, strerror(errno)); exit(1); default: /* parent */ exit(0); } } if (IS_STDOUT(device)) { output = stdout; need_close_output = 0; } else if (IS_STDERR(device)) { output = stderr; need_close_output = 0; } if (need_close_output) { // i.e, output neither stdout or stderr output = fopen(device, "w"); if (! output) { fprintf( stderr, "%s: can't open '%s' - %s\n", pgm_name, device, strerror(errno) ); exit(1); } } #define CURRENT operations[index] for (index = 0; index < op_number; index++) { switch(CURRENT.type) { case type_ascii: { const char *str; for (str = CURRENT.op.ascii; *str; str++) { result = kbde_ascii_key_press_release((int)(str[0]), buf); if (result > 0) { fprintf(output, "%s", buf); fflush(output); usleep(CURRENT.delay); } } } break; case type_key: result = kbde_key_press_release(CURRENT.op.key_code, buf); if (result > 0) { fprintf(output, "%s", buf); fflush(output); usleep(CURRENT.delay); } break; case type_key_press: result = kbde_key_press(CURRENT.op.key_code, buf); if (result > 0) { fprintf(output, "%s", buf); fflush(output); usleep(CURRENT.delay); } break; case type_key_release: result = kbde_key_release(CURRENT.op.key_code, buf); if (result > 0) { fprintf(output, "%s", buf); fflush(output); usleep(CURRENT.delay); } break; case type_file: process_input_file(CURRENT.op.filename, CURRENT.delay, output); break; case type_sleep: usleep(CURRENT.op.sleep); break; } } exit(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -