예제 #1
0
int main()
{
    InterceptionContext context;
    InterceptionDevice device;
    InterceptionStroke stroke;

    raise_process_priority();

    context = interception_create_context();

    interception_set_filter(context, interception_is_keyboard, INTERCEPTION_FILTER_KEY_DOWN | INTERCEPTION_FILTER_KEY_UP);
    interception_set_filter(context, interception_is_mouse, INTERCEPTION_FILTER_MOUSE_MOVE);

    while(interception_receive(context, device = interception_wait(context), &stroke, 1) > 0)
    {
        if(interception_is_mouse(device))
        {
            InterceptionMouseStroke &mstroke = *(InterceptionMouseStroke *) &stroke;

            if(!(mstroke.flags & INTERCEPTION_MOUSE_MOVE_ABSOLUTE)) mstroke.y *= -1;

            interception_send(context, device, &stroke, 1);
        }

        if(interception_is_keyboard(device))
        {
            InterceptionKeyStroke &kstroke = *(InterceptionKeyStroke *) &stroke;

            interception_send(context, device, &stroke, 1);

            if(kstroke.code == SCANCODE_ESC) break;
        }
    }

    interception_destroy_context(context);

    return 0;
}
DWORD WINAPI KeyScannerThread( LPVOID lpParam )
{
	GlobalStore.context = interception_create_context();

	InterceptionDevice		LastDevice;
	InterceptionStroke		LastStroke;
	int		PrevKeyEventTick = GetTickCount();
	int		PrevMouseEventTick = GetTickCount();

	bool	HasKeyboardHandlers = false;
	bool	HasMouseHandlers = false;

	std::list<IrcGameKeyStore*>::iterator itr;
	for( itr=GlobalStore.MonitoredKeys.begin(); itr!=GlobalStore.MonitoredKeys.end(); itr++ )
	{
		if( (*itr)->StrokeCode != 0 )
			HasKeyboardHandlers = true;
		if( (*itr)->MouseX != 0 || (*itr)->MouseY != 0 || (*itr)->MouseKey != 0 )
			HasMouseHandlers = true;
	}

	if( GlobalStore.PrintKeysPressed == true )
	{
		HasKeyboardHandlers = true;
		HasMouseHandlers = true;
	}

	if( HasKeyboardHandlers )
		interception_set_filter( GlobalStore.context, interception_is_keyboard, INTERCEPTION_FILTER_KEY_DOWN | INTERCEPTION_FILTER_KEY_UP);
	if( HasMouseHandlers )
		interception_set_filter( GlobalStore.context, interception_is_mouse, INTERCEPTION_FILTER_MOUSE_MOVE | INTERCEPTION_MOUSE_LEFT_BUTTON_DOWN | INTERCEPTION_MOUSE_LEFT_BUTTON_UP | INTERCEPTION_MOUSE_RIGHT_BUTTON_DOWN | INTERCEPTION_MOUSE_RIGHT_BUTTON_UP );
//		interception_set_filter( GlobalStore.context, interception_is_mouse, INTERCEPTION_FILTER_MOUSE_ALL );
	while( interception_receive( GlobalStore.context, LastDevice = interception_wait( GlobalStore.context ), (InterceptionStroke *)&LastStroke, 1) > 0
		&& GlobalStore.WorkerThreadAlive == 1
//		&& 0
		)
	{
		if( interception_is_mouse( LastDevice ) )
		{
			GlobalStore.MouseDevice = LastDevice;
            InterceptionMouseStroke &mstroke = *(InterceptionMouseStroke *) &LastStroke;
			
			memcpy( &GlobalStore.MouseStroke, &LastStroke, sizeof( InterceptionMouseStroke ) );

			//try to track mouse coordinate
            if( (mstroke.flags & INTERCEPTION_MOUSE_MOVE_ABSOLUTE) == 0 )
			{
				//this can go off screen
				GlobalStore.TrackedMouseX += mstroke.x;
				if( GlobalStore.TrackedMouseX > GlobalStore.DosBoxWidth )
					GlobalStore.TrackedMouseX = GlobalStore.DosBoxWidth;
				if( GlobalStore.TrackedMouseX < 0 )
					GlobalStore.TrackedMouseX = 0;
				GlobalStore.TrackedMouseY += mstroke.y;
				if( GlobalStore.TrackedMouseY > GlobalStore.DosBoxHeight )
					GlobalStore.TrackedMouseY = GlobalStore.DosBoxHeight;
				if( GlobalStore.TrackedMouseY < 0 )
					GlobalStore.TrackedMouseY = 0;
			}

			if( GlobalStore.StartedRecording == 1 )
			{
				//track mouse positions and on click try to print it out
				GlobalStore.TrackedMouseScriptX += mstroke.x;
				if( GlobalStore.TrackedMouseScriptX > GlobalStore.MouseXLimitMax )
					GlobalStore.TrackedMouseScriptX = GlobalStore.MouseXLimitMax;
				if( GlobalStore.TrackedMouseScriptX < GlobalStore.MouseXLimitMin )
					GlobalStore.TrackedMouseScriptX = GlobalStore.MouseXLimitMin;
				GlobalStore.TrackedMouseScriptY += mstroke.y;
				if( GlobalStore.TrackedMouseScriptY > GlobalStore.MouseYLimitMax )
					GlobalStore.TrackedMouseScriptY = GlobalStore.MouseYLimitMax;
				if( GlobalStore.TrackedMouseScriptY < GlobalStore.MouseYLimitMin )
					GlobalStore.TrackedMouseScriptY = GlobalStore.MouseYLimitMin;

				if( mstroke.state != 0 )
				{
					int TimePassed = GetTickCount() - GlobalStore.TrackedMouseScriptStamp;
					GlobalStore.TrackedMouseScriptStamp = GetTickCount();
					POINT p;
					GetCursorPos( &p );
//					printf("Registering click at (%d,%d), Time spent %d. Win said %d,%d\n", GlobalStore.TrackedMouseScriptX, GlobalStore.TrackedMouseScriptY, TimePassed, p.x, p.y );
					printf("click (%d,%d) - %d.\n", p.x, p.y, TimePassed );
//					GlobalStore.TrackedMouseScriptX = p.x;
//					GlobalStore.TrackedMouseScriptY = p.y;
				}
			}

            interception_send( GlobalStore.context, GlobalStore.MouseDevice, (InterceptionStroke *)&mstroke, 1);

			if( GlobalStore.PrintKeysPressed )
			{
//				printf( "Mouse %d: state %d, x %d y %d, flags %d, rolling %d, info %d, tracked %d %d\n", GetTickCount() - PrevKeyEventTick, mstroke.state, mstroke.x, mstroke.y, mstroke.flags, mstroke.rolling, mstroke.information, GlobalStore.TrackedMouseX, GlobalStore.TrackedMouseY );
				printf( "Mouse %d: state %d, x %d y %d, flags %d, rolling %d, info %d\n", GetTickCount() - PrevKeyEventTick, mstroke.state, mstroke.x, mstroke.y, mstroke.flags, mstroke.rolling, mstroke.information );
				PrevKeyEventTick = GetTickCount();
			}
		}
		if( interception_is_keyboard( LastDevice ) )
		{
			GlobalStore.KeyboardDevice = LastDevice;
            InterceptionKeyStroke &kstroke = *(InterceptionKeyStroke *) &LastStroke;

			memcpy( &GlobalStore.KeyboardStroke, &LastStroke, sizeof( InterceptionKeyStroke ) );

			interception_send( GlobalStore.context, GlobalStore.KeyboardDevice, (InterceptionStroke *)&LastStroke, 1);

			if( GlobalStore.PrintKeysPressed )
			{
				printf( "Keyboard delay %d: scan code %d %d %d\n", GetTickCount() - PrevKeyEventTick, kstroke.code, kstroke.state, kstroke.information );
				PrevKeyEventTick = GetTickCount();
			}
			else if( kstroke.state == 0 ) //pushdown
			{
				HWND FW = GetForegroundWindow( );
				HWND CW = GetConsoleWindow();
				if( CW == FW )
					printf( "(%d)", kstroke.code );
			}

			if( kstroke.code == GlobalStore.PauseToggleKeyCode && kstroke.state == 0 )
			{
				GlobalStore.PauseSendKeys = 1 - GlobalStore.PauseSendKeys;
				printf( "KeySending thread pause state changed to %d\n", GlobalStore.PauseSendKeys );
				std::list<IrcGameKeyStore*>::iterator itr;
				for( itr=GlobalStore.MonitoredKeys.begin(); itr!=GlobalStore.MonitoredKeys.end(); itr++ )
					if( (*itr)->PushInterval == ONE_TIME_PUSH_KEY_INTERVAL )
					{
						(*itr)->PushInterval = 0;
						(*itr)->LastPushStamp = GetTickCount();
					}

			}

			if( kstroke.code == 10 && kstroke.state == 0 )
			{
				GlobalStore.StartedRecording = 1 - GlobalStore.StartedRecording;
				if( GlobalStore.StartedRecording == 1 )
				{
					//bring mouse to 0 0
//					SendMouseChange( 0, 0, -10000, -10000, SLEEP_BETWEEN_KEYPRESS, SLEEP_BETWEEN_KEYPRESS );
//					SendMouseChange( 1, 0, -10000, -10000, SLEEP_BETWEEN_KEYPRESS, SLEEP_BETWEEN_KEYPRESS );
//					SendMouseChange( 1, 0, 0, 0, SLEEP_BETWEEN_KEYPRESS, SLEEP_BETWEEN_KEYPRESS );
					GlobalStore.TrackedMouseScriptX = 0;
					GlobalStore.TrackedMouseScriptY = 0;
					printf("Key 9 was pressed. Moved mouse to 0,0 so we can start recording mouse movement for scripts. Not accurate !\n");
				}
			}

			if( GlobalStore.StartedRecording == 1 )
			{
				if( kstroke.state != 0 )
				{
					int TimePassed = GetTickCount() - GlobalStore.TrackedMouseScriptStamp;
					GlobalStore.TrackedMouseScriptStamp = GetTickCount();
					POINT p;
					GetCursorPos( &p );
					printf("click (%d,%d) - %d.\n", p.x, p.y, TimePassed );
				}
			}

			if( kstroke.code == SCANCODE_CONSOLE )
			{
				GlobalStore.WorkerThreadAlive = 0;
				printf( "Esc pressed. Shutting down\n" );
				break;
			}
		}
	}/**/

	//this happens in case laptop mouse goes idle. It might come back later = Never give up hope !
	if( GlobalStore.WorkerThreadAlive == 1 )
	{
		printf("Worker thread keymonitor device failure. Trying to work with remaining devices...\n");
		while( GlobalStore.WorkerThreadAlive == 1 )
			Sleep( 1000 );
	}
	interception_destroy_context( GlobalStore.context );
	printf("Worker thread keymonitor exited\n");

	return 0;
}