Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <cstdio> // printf
- #include <cstdlib> // atoi
- #include <unistd.h> // getopt
- bool KeyIsPressed ( unsigned char k )
- {
- USHORT status = GetAsyncKeyState(k);
- return (((status & 0x8000) >> 15) == 1) || ((status & 1) == 1);
- }
- int click ( INPUT input ) {
- input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
- SendInput( 1, &input, sizeof( INPUT ) );
- ZeroMemory( &input, sizeof( INPUT ) );
- input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
- SendInput( 1, &input, sizeof( INPUT ) );
- printf( "." );
- return 0;
- }
- int click ( unsigned char key, bool toggle, int sleep ) {
- INPUT input;
- input.type = INPUT_MOUSE;
- bool active = false;
- bool down_prev = false;
- bool down_now = false;
- while ( true ) {
- if ( toggle ) {
- down_now = KeyIsPressed( key );
- if (down_prev && !down_now) {
- active = !active;
- }
- down_prev = down_now;
- if ( active ) {
- click( input );
- }
- } else if ( KeyIsPressed( key ) ) {
- click( input );
- }
- Sleep( sleep );
- }
- return 0;
- }
- // Using main in favor of winmain to allow easier use of command line options
- int __cdecl main( int argc, char *argv[] ) {
- char key = 0x45;
- bool toggle = false;
- int sleep = 10;
- int opt;
- while ( ( opt = getopt( argc, argv, "k:s:t" ) ) != -1 ) {
- switch (opt) {
- case 'k': // KEY
- key = optarg[0];
- if ( key > 0x60 && key < 0x7b ) {
- key -= 0x20;
- }
- break;
- case 's': // SLEEP
- sleep = atoi( optarg );
- if ( sleep < 1) {
- sleep = 1;
- }
- break;
- case 't': // TOGGLE
- toggle = true;
- break;
- default:
- break;
- }
- }
- printf( "Key: %c\n", key);
- printf( "Toggle: %s\n", toggle ? "yes" : "no" );
- printf( "Sleep: %i\n", sleep );
- click( key, toggle, sleep );
- return 0;
- }
Add Comment
Please, Sign In to add comment