Пример #1
0
int interception_receive(InterceptionContext context, InterceptionDevice device, InterceptionStroke *stroke, unsigned int nstroke)
{
    InterceptionDeviceArray device_array = (InterceptionDeviceArray)context;
    DWORD strokesread = 0;

	

    if(context == 0 || nstroke == 0 || interception_is_invalid(device)) return 0;

    if(interception_is_keyboard(device))
    {
        PKEYBOARD_INPUT_DATA rawstrokes = (PKEYBOARD_INPUT_DATA)HeapAlloc(GetProcessHeap(), 0, nstroke * sizeof(KEYBOARD_INPUT_DATA));
        unsigned int i;

        if(!rawstrokes) return 0;

        DeviceIoControl(device_array[device - 1].handle, IOCTL_READ, NULL, 0, rawstrokes, (DWORD)nstroke * sizeof(KEYBOARD_INPUT_DATA), &strokesread, NULL);

        strokesread /= sizeof(KEYBOARD_INPUT_DATA);

        for(i = 0; i < (unsigned int)strokesread; ++i)
        {
            InterceptionKeyStroke *key_stroke = (InterceptionKeyStroke *) stroke;
            
            key_stroke[i].code = rawstrokes[i].MakeCode;
            key_stroke[i].state = rawstrokes[i].Flags;
            key_stroke[i].information = rawstrokes[i].ExtraInformation;
        }

        HeapFree(GetProcessHeap(), 0,  rawstrokes);
    }
    else
    {
        PMOUSE_INPUT_DATA rawstrokes = (PMOUSE_INPUT_DATA)HeapAlloc(GetProcessHeap(), 0, nstroke * sizeof(MOUSE_INPUT_DATA));
        unsigned int i;

        if(!rawstrokes) return 0;

        DeviceIoControl(device_array[device - 1].handle, IOCTL_READ, NULL, 0, rawstrokes, (DWORD)nstroke * sizeof(MOUSE_INPUT_DATA), &strokesread, NULL);

        strokesread /= sizeof(MOUSE_INPUT_DATA);

        for(i = 0; i < (unsigned int)strokesread; ++i)
        {
            InterceptionMouseStroke *mouse_stroke = (InterceptionMouseStroke *) stroke;
            
            mouse_stroke[i].flags = rawstrokes[i].Flags;
            mouse_stroke[i].state = rawstrokes[i].ButtonFlags;
            mouse_stroke[i].rolling = rawstrokes[i].ButtonData;
            mouse_stroke[i].x = rawstrokes[i].LastX;
            mouse_stroke[i].y = rawstrokes[i].LastY;
            mouse_stroke[i].information = rawstrokes[i].ExtraInformation;
        }

        HeapFree(GetProcessHeap(), 0,  rawstrokes);
    }

    return strokesread;
}
Пример #2
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;
}
Пример #3
0
int interception_is_invalid(InterceptionDevice device)
{
    return !interception_is_keyboard(device) && !interception_is_mouse(device);
}
Пример #4
0
int interception_send(InterceptionContext context, InterceptionDevice device, const InterceptionStroke *stroke, unsigned int nstroke)
{
    InterceptionDeviceArray device_array = (InterceptionDeviceArray)context;
    DWORD strokeswritten = 0;

    if(context == 0 || nstroke == 0 || interception_is_invalid(device) || !device_array[device - 1].handle) return 0;

    if(interception_is_keyboard(device))
    {
        PKEYBOARD_INPUT_DATA rawstrokes = (PKEYBOARD_INPUT_DATA)HeapAlloc(GetProcessHeap(), 0, nstroke * sizeof(KEYBOARD_INPUT_DATA));
        unsigned int i;

        if(!rawstrokes) return 0;

        for(i = 0; i < nstroke; ++i)
        {
            InterceptionKeyStroke *key_stroke = (InterceptionKeyStroke *) stroke;

            rawstrokes[i].UnitId = 0;
            rawstrokes[i].MakeCode = key_stroke[i].code;
            rawstrokes[i].Flags = key_stroke[i].state;
            rawstrokes[i].Reserved = 0;
            rawstrokes[i].ExtraInformation = key_stroke[i].information;
        }

        DeviceIoControl(device_array[device - 1].handle, IOCTL_WRITE, rawstrokes,(DWORD)nstroke * sizeof(KEYBOARD_INPUT_DATA), NULL, 0, &strokeswritten, NULL);

        HeapFree(GetProcessHeap(), 0,  rawstrokes);

        strokeswritten /= sizeof(KEYBOARD_INPUT_DATA);
    }
    else
    {
        PMOUSE_INPUT_DATA rawstrokes = (PMOUSE_INPUT_DATA)HeapAlloc(GetProcessHeap(), 0, nstroke * sizeof(MOUSE_INPUT_DATA));
        unsigned int i;

        if(!rawstrokes) return 0;

        for(i = 0; i < nstroke; ++i)
        {
            InterceptionMouseStroke *mouse_stroke = (InterceptionMouseStroke *) stroke;

            rawstrokes[i].UnitId = 0;
            rawstrokes[i].Flags = mouse_stroke[i].flags;
            rawstrokes[i].ButtonFlags = mouse_stroke[i].state;
            rawstrokes[i].ButtonData = mouse_stroke[i].rolling;
            rawstrokes[i].RawButtons = 0;
            rawstrokes[i].LastX = mouse_stroke[i].x;
            rawstrokes[i].LastY = mouse_stroke[i].y;
            rawstrokes[i].ExtraInformation = mouse_stroke[i].information;
        }

        DeviceIoControl(device_array[device - 1].handle, IOCTL_WRITE, rawstrokes, (DWORD)nstroke * sizeof(MOUSE_INPUT_DATA), NULL, 0, &strokeswritten, NULL);

        HeapFree(GetProcessHeap(), 0,  rawstrokes);

        strokeswritten /= sizeof(MOUSE_INPUT_DATA);
    }

    return strokeswritten;
}
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;
}