]>
git.gir.st - sendHID.git/blob - main.c
2 description: sends a sequence of keystrokes to the hid device.
4 device file (e.g. /dev/hidg0)
5 keyboard layout (1=en_us, 2=de_at, 3=de_at-nodeadkeys)
6 unicode method: 1=gtk_holddown, 2=gtk_spaceend, 3=windows
7 the string to send (as whitespace is important, the `echo` way of concatenating all parameters is not supported. if your string has white space in it and you are in an interactive session, quote your string.)
12 #include "scancodes.h"
15 #define P_EXE 0 //executable name
16 #define P_DEV 1 //device file
17 #define P_LAY 2 //layout
18 #define P_UNI 3 //unicode method
19 #define P_STR 4 //string to type
20 #define NUM_P P_STR+1 //number of parameters
22 void send_key ( FILE * hid_dev
, unsigned short key
, unsigned short mod
);
24 int main ( int argc
, char ** argv
) {
26 fprintf ( stderr
, "Usage: %s <device file> <layout> <unicode> \" <string> \"\n " , argv
[ P_EXE
]);
29 FILE * hid_dev
= fopen ( "/dev/hidg0" , "w" );
30 for ( int i
= 0 ; i
< strlen ( argv
[ P_STR
]); i
++) {
32 char tmp
[ UTF8_MAX_LENGTH
] = { argv
[ P_STR
][ i
], argv
[ P_STR
][ i
+ 1 ], argv
[ P_STR
][ i
+ 2 ], '\0' };
33 //TODO: replace by something less stupid
34 if ( argv
[ P_STR
][ i
] < 128 ) { // not multi byte
36 } else { // is multi byte
37 if ( argv
[ P_STR
][ i
] < 0xe0 ) {
38 i
++; //skip next thing
41 i
+= 2 ; //WARN: fails on utf8 > 3 byte
45 struct keysym
* s
= toscan ( tmp
);
47 fprintf ( stderr
, "Key Symbol not found. \n " );
51 int ignore_deadkey
= 0 ;
52 switch ( atoi ( argv
[ P_LAY
])) {
54 fprintf ( stderr
, "This keyboard layout is reserved. \n " );
62 case 3 : //de_at-nodeadkeys
67 fprintf ( stderr
, "Unrecognised keyboard layout. \n " );
71 //key does not exist in this layout
72 fprintf ( stderr
, "Key not in this layout! \n " );
73 /*TODO: send unicode sequence
74 there are different methods to be used for gtk and
75 winblows. ctrl-shift-u-HEX vs. ctrl-shift-u,HEX,SPACE
76 vs. alt+NUMPAD vs. alt+'+'+HEX
78 switch ( atoi ( argv
[ P_UNI
])) {
79 case 0 : //skip unicode character entry
81 case 1 : //gtk: hold ctrl and shift while entering
84 case 2 : //gtk: use space as end marker for unicode
86 case 3 : //windows: alt+numpad (decimal)
90 fprintf ( stderr
, "Unicode Method unknown! \n " );
94 send_key ( hid_dev
, l
-> key
, l
-> mod
);
95 send_key ( hid_dev
, '\0' , '\0' ); //release all keys
96 if ( l
-> is_dead
&& ! ignore_deadkey
) {
97 //dead keys need to be pressed twice to show up
98 send_key ( hid_dev
, l
-> key
, l
-> mod
);
99 send_key ( hid_dev
, '\0' , '\0' ); //release all keys
108 void send_key ( FILE * hid_dev
, unsigned short key
, unsigned short mod
) {
109 fprintf ( hid_dev
, "%c%c%c%c%c%c%c%c" , mod
, '\0' , key
, '\0' , '\0' , '\0' , '\0' , '\0' );