void CIrrDeviceMarmalade::onCharEvent(s3eKeyboardCharEvent *evt)
	{
		SEvent irrEvt;
		irrEvt.EventType = EET_KEY_INPUT_EVENT;
		irrEvt.KeyInput.Char = evt->m_Char;
		irrEvt.KeyInput.Key = (EKEY_CODE)0;
		irrEvt.KeyInput.PressedDown = 1;
		irrEvt.KeyInput.Control = 0;
		irrEvt.KeyInput.Shift = 0;
		postEventFromUser(irrEvt);
		irrEvt.KeyInput.PressedDown = 0;
		postEventFromUser(irrEvt);
	}
void CengineDeviceWin32::__lte_send_po(EKEY_CODE a)
{
	engine::SEvent ev;
	ev.KeyInput.Char = 0;
	ev.EventType = engine::EET_KEY_INPUT_EVENT;
	ev.KeyInput.PressedDown = false;
	ev.KeyInput.Key = a;
	ev.KeyInput.PressedOnce = true;
	postEventFromUser(ev);
}
//! runs the device. Returns false if device wants to be deleted
bool CIrrDeviceFB::run()
{
	os::Timer::tick();

	struct input_event ev;
	if (EventDevice>=0)
	{
		if ((read(EventDevice, &ev, sizeof(input_event)) < 0) &&
				errno != EAGAIN)
			perror("Read input event");
		if (ev.type == EV_KEY)
		{
			irr::SEvent irrevent;
			irrevent.EventType = irr::EET_KEY_INPUT_EVENT;
			irrevent.KeyInput.PressedDown = true;
			
			switch (ev.code)
			{
				case KEY_RIGHTCTRL:
				case KEY_LEFTCTRL:
					irrevent.KeyInput.Control = true;
				break;
				case KEY_RIGHTSHIFT:
				case KEY_LEFTSHIFT:
					irrevent.KeyInput.Shift = true;
				break;
				case KEY_ESC:
					irrevent.KeyInput.Key = (EKEY_CODE)0x1B;
				break;
				case KEY_SPACE:
					irrevent.KeyInput.Key = (EKEY_CODE)0x20;
				break;
				case KEY_UP:
					irrevent.KeyInput.Key = (EKEY_CODE)0x26;
				break;
				case KEY_LEFT:
					irrevent.KeyInput.Key = (EKEY_CODE)0x25;
				break;
				case KEY_RIGHT:
					irrevent.KeyInput.Key = (EKEY_CODE)0x27;
				break;
				case KEY_DOWN:
					irrevent.KeyInput.Key = (EKEY_CODE)0x28;
				break;
				default:
					irrevent.KeyInput.Key = (EKEY_CODE)0;
				break;
			}
			postEventFromUser(irrevent);
		}
	}

	return !Close;
}
	void CIrrDeviceMarmalade::onKeyEvent(s3eKeyboardEvent *evt)
	{
		SEvent irrEvt;
		irrEvt.EventType = EET_KEY_INPUT_EVENT;
		irrEvt.KeyInput.Char = 0;
		irrEvt.KeyInput.Key = irr_key_from_s3e[evt->m_Key];
		irrEvt.KeyInput.PressedDown = !!evt->m_Pressed;
		irrEvt.KeyInput.Control = 0;
		irrEvt.KeyInput.Shift = 0;
		postEventFromUser(irrEvt);
	}
//! runs the device. Returns false if device wants to be deleted
bool CIrrDeviceFB::run()
{
	os::Timer::tick();

	struct input_event ev;
	if (EventDevice>=0)
	{
		if ((read(EventDevice, &ev, sizeof(input_event)) < 0) &&
				errno != EAGAIN)
			perror("Read input event");
		if (ev.type == EV_KEY)
		{
			irr::SEvent irrevent;
			irrevent.EventType = irr::EET_KEY_INPUT_EVENT;
			irrevent.KeyInput.PressedDown = (ev.value == 1);

			switch (ev.code)
			{
				case KEY_RIGHTCTRL:
				case KEY_LEFTCTRL:
					irrevent.KeyInput.Control = true;
				break;
				case KEY_RIGHTSHIFT:
				case KEY_LEFTSHIFT:
					irrevent.KeyInput.Shift = true;
				break;
				case KEY_ESC:
					irrevent.KeyInput.Key = (EKEY_CODE)0x1B;
				break;
				case KEY_SPACE:
					irrevent.KeyInput.Key = (EKEY_CODE)0x20;
				break;
				case KEY_UP:
					irrevent.KeyInput.Key = (EKEY_CODE)0x26;
				break;
				case KEY_LEFT:
					irrevent.KeyInput.Key = (EKEY_CODE)0x25;
				break;
				case KEY_RIGHT:
					irrevent.KeyInput.Key = (EKEY_CODE)0x27;
				break;
				case KEY_DOWN:
					irrevent.KeyInput.Key = (EKEY_CODE)0x28;
				break;
				case KEY_A:
					irrevent.KeyInput.Key = (EKEY_CODE)0x41;
				break;
				case KEY_B:
					irrevent.KeyInput.Key = (EKEY_CODE)0x42;
				break;
				case KEY_C:
					irrevent.KeyInput.Key = (EKEY_CODE)0x43;
				break;
				case KEY_D:
					irrevent.KeyInput.Key = (EKEY_CODE)0x44;
				break;
				case KEY_E:
					irrevent.KeyInput.Key = (EKEY_CODE)0x45;
				break;
				case KEY_F:
					irrevent.KeyInput.Key = (EKEY_CODE)0x46;
				break;
				case KEY_G:
					irrevent.KeyInput.Key = (EKEY_CODE)0x47;
				break;
				case KEY_H:
					irrevent.KeyInput.Key = (EKEY_CODE)0x48;
				break;
				case KEY_I:
					irrevent.KeyInput.Key = (EKEY_CODE)0x49;
				break;
				case KEY_J:
					irrevent.KeyInput.Key = (EKEY_CODE)0x4A;
				break;
				case KEY_K:
					irrevent.KeyInput.Key = (EKEY_CODE)0x4B;
				break;
				case KEY_L:
					irrevent.KeyInput.Key = (EKEY_CODE)0x4C;
				break;
				case KEY_M:
					irrevent.KeyInput.Key = (EKEY_CODE)0x4D;
				break;
				case KEY_N:
					irrevent.KeyInput.Key = (EKEY_CODE)0x4E;
				break;
				case KEY_O:
					irrevent.KeyInput.Key = (EKEY_CODE)0x4F;
				break;
				case KEY_P:
					irrevent.KeyInput.Key = (EKEY_CODE)0x50;
				break;
				case KEY_Q:
					irrevent.KeyInput.Key = (EKEY_CODE)0x51;
				break;
				case KEY_R:
					irrevent.KeyInput.Key = (EKEY_CODE)0x52;
				break;
				case KEY_S:
					irrevent.KeyInput.Key = (EKEY_CODE)0x53;
				break;
				case KEY_T:
					irrevent.KeyInput.Key = (EKEY_CODE)0x54;
				break;
				case KEY_U:
					irrevent.KeyInput.Key = (EKEY_CODE)0x55;
				break;
				case KEY_V:
					irrevent.KeyInput.Key = (EKEY_CODE)0x56;
				break;
				case KEY_W:
					irrevent.KeyInput.Key = (EKEY_CODE)0x57;
				break;
				case KEY_X:
					irrevent.KeyInput.Key = (EKEY_CODE)0x58;
				break;
				case KEY_Y:
					irrevent.KeyInput.Key = (EKEY_CODE)0x59;
				break;
				case KEY_Z:
					irrevent.KeyInput.Key = (EKEY_CODE)0x5A;
				break;
				default:
					irrevent.KeyInput.Key = (EKEY_CODE)0;
				break;
			}
			postEventFromUser(irrevent);
		}
	}

	return !Close;
}
//! runs the device. Returns false if device wants to be deleted
bool CIrrDeviceConsole::run()
{
	// increment timer
	os::Timer::tick();

	// process Windows console input
#ifdef _IRR_WINDOWS_NT_CONSOLE_

	INPUT_RECORD in;
	DWORD        oldMode;
	DWORD        count, waste;

	// get old input mode
	GetConsoleMode(WindowsSTDIn, &oldMode);
	SetConsoleMode(WindowsSTDIn, ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT);

	GetNumberOfConsoleInputEvents(WindowsSTDIn, &count);

	// read keyboard and mouse input
	while (count)
	{
		ReadConsoleInput(WindowsSTDIn, &in, 1, &waste );
		switch(in.EventType)
		{
		case KEY_EVENT:
		{
			SEvent e;
			e.EventType            = EET_KEY_INPUT_EVENT;
			e.KeyInput.PressedDown = (in.Event.KeyEvent.bKeyDown == TRUE);
			e.KeyInput.Control     = (in.Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) != 0;
			e.KeyInput.Shift       = (in.Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) != 0;
			e.KeyInput.Key         = EKEY_CODE(in.Event.KeyEvent.wVirtualKeyCode);
			e.KeyInput.Char        = in.Event.KeyEvent.uChar.UnicodeChar;
			postEventFromUser(e);
			break;
		}
		case MOUSE_EVENT:
		{
			SEvent e;
			e.EventType        = EET_MOUSE_INPUT_EVENT;
			e.MouseInput.X     = in.Event.MouseEvent.dwMousePosition.X;
			e.MouseInput.Y     = in.Event.MouseEvent.dwMousePosition.Y;
			e.MouseInput.Wheel = 0.f;
			e.MouseInput.ButtonStates =
				( (in.Event.MouseEvent.dwButtonState & FROM_LEFT_1ST_BUTTON_PRESSED) ? EMBSM_LEFT   : 0 ) |
				( (in.Event.MouseEvent.dwButtonState & RIGHTMOST_BUTTON_PRESSED)     ? EMBSM_RIGHT  : 0 ) |
				( (in.Event.MouseEvent.dwButtonState & FROM_LEFT_2ND_BUTTON_PRESSED) ? EMBSM_MIDDLE : 0 ) |
				( (in.Event.MouseEvent.dwButtonState & FROM_LEFT_3RD_BUTTON_PRESSED) ? EMBSM_EXTRA1 : 0 ) |
				( (in.Event.MouseEvent.dwButtonState & FROM_LEFT_4TH_BUTTON_PRESSED) ? EMBSM_EXTRA2 : 0 );

			if (in.Event.MouseEvent.dwEventFlags & MOUSE_MOVED)
			{
				CursorControl->setPosition(core::position2di(e.MouseInput.X, e.MouseInput.Y));

				// create mouse moved event
				e.MouseInput.Event = EMIE_MOUSE_MOVED;
				postEventFromUser(e);
			}

			if (in.Event.MouseEvent.dwEventFlags & MOUSE_WHEELED)
			{
				e.MouseInput.Event = EMIE_MOUSE_WHEEL;
				e.MouseInput.Wheel = (in.Event.MouseEvent.dwButtonState & 0xFF000000) ? -1.0f : 1.0f;
				postEventFromUser(e);
			}

			if ( (MouseButtonStates & EMBSM_LEFT) != (e.MouseInput.ButtonStates & EMBSM_LEFT) )
			{
				e.MouseInput.Event = (e.MouseInput.ButtonStates & EMBSM_LEFT) ? EMIE_LMOUSE_PRESSED_DOWN : EMIE_LMOUSE_LEFT_UP;
				postEventFromUser(e);
			}

			if ( (MouseButtonStates & EMBSM_RIGHT) != (e.MouseInput.ButtonStates & EMBSM_RIGHT) )
			{
				e.MouseInput.Event = (e.MouseInput.ButtonStates & EMBSM_RIGHT) ? EMIE_RMOUSE_PRESSED_DOWN : EMIE_RMOUSE_LEFT_UP;
				postEventFromUser(e);
			}

			if ( (MouseButtonStates & EMBSM_MIDDLE) != (e.MouseInput.ButtonStates & EMBSM_MIDDLE) )
			{
				e.MouseInput.Event = (e.MouseInput.ButtonStates & EMBSM_MIDDLE) ? EMIE_MMOUSE_PRESSED_DOWN : EMIE_MMOUSE_LEFT_UP;
				postEventFromUser(e);
			}

			// save current button states
			MouseButtonStates = e.MouseInput.ButtonStates;

			break;
		}
		case WINDOW_BUFFER_SIZE_EVENT:
			VideoDriver->OnResize(
				core::dimension2d<u32>(in.Event.WindowBufferSizeEvent.dwSize.X,
				                       in.Event.WindowBufferSizeEvent.dwSize.Y));
			break;
		case FOCUS_EVENT:
			IsWindowFocused = (in.Event.FocusEvent.bSetFocus == TRUE);
			break;
		default:
			break;
		}
		GetNumberOfConsoleInputEvents(WindowsSTDIn, &count);
	}

	// set input mode
	SetConsoleMode(WindowsSTDIn, oldMode);
#else
	// todo: keyboard input from terminal in raw mode
#endif

	return !Close;
}
//! runs the device. Returns false if device wants to be deleted
bool CengineDeviceWin32::run()
{
	if (__engine_stop_execution) return false;

	os::Timer::tick();

	if(NetworkDriver)
		NetworkDriver->run();

	bool quit = false;
	engine::SEvent event;
	event.KeyInput.Control = false;

	// *------------------------------------------
	// tutte le operazioni per prendere il cursore
	// e i tasti premuti vanno qui
	SceCtrlData pad;
	sceCtrlPeekBufferPositive (&pad, 1);


	// la tastiera è attiva
	if (key_state == 1) {

		if (pad.Buttons & PSP_CTRL_UP)
			key_pos_y -= 5;

		if (pad.Buttons & PSP_CTRL_DOWN)
			key_pos_y += 5;

		if (pad.Buttons & PSP_CTRL_RIGHT)
			key_pos_x += 5;

		if (pad.Buttons & PSP_CTRL_LEFT)
			key_pos_x -= 5;

		// disabilita la keyboard
		if (pad.Buttons & PSP_CTRL_START)
		{
			if (wasStartDown == 0) {
				key_state = 0;
				wasStartDown = 1;
				return !quit;
			}
		} else {
			wasStartDown = 0;
		}



		/* aggiusta la posizione in caso */
		if (key_pos_x < 0) key_pos_x = 0;
		if (key_pos_x > 329) key_pos_x = 329;
		if (key_pos_y < 0) key_pos_y = 0;
		if (key_pos_y > 121) key_pos_y = 121;

		if (pad.Buttons & PSP_CTRL_LTRIGGER){
			if (wasLDown == 0){
				key_type--;
				wasLDown = 1;
			}
		} else wasLDown = 0;

		if (pad.Buttons & PSP_CTRL_RTRIGGER){
			if (wasRDown == 0) {
				key_type++;
				wasRDown = 1;
			}
		} else wasRDown = 0;

		if (key_type < 0) key_type = 3;
		if (key_type > 3) key_type = 0;





		int xpos = 0, ypos = 0;
		if ((pad.Lx > 170) || (pad.Lx < 100)) {
			xpos = (pad.Lx - 128);
		}
		if ((pad.Ly > 170) || (pad.Ly < 100)) {
			ypos = (pad.Ly - 128);
		}

		int currentindex = 4;
		if (xpos < 0) currentindex--;
		if (xpos > 0) currentindex++;
		if (ypos > 0) currentindex+=3;
		if (ypos < 0) currentindex-=3;
		key_sel = currentindex;

		int keycode = KEYMAP_NOTHING;

		if (pad.Buttons & PSP_CTRL_SQUARE) keycode = key_map_of_codes[key_type][key_sel][0];
		if (pad.Buttons & PSP_CTRL_TRIANGLE) keycode = key_map_of_codes[key_type][key_sel][1];
		if (pad.Buttons & PSP_CTRL_CIRCLE) keycode = key_map_of_codes[key_type][key_sel][2];
		if (pad.Buttons & PSP_CTRL_CROSS) keycode = key_map_of_codes[key_type][key_sel][3];


		if (keymapPressed == 0)
			switch (keycode) {
			case KEYMAP_NOTHING:
				break;
			case KEYMAP_DELETE:
				event.KeyInput.Key = engine::KEY_BACK;
				event.EventType = engine::EET_KEY_INPUT_EVENT;
				event.KeyInput.PressedDown = true;
				postEventFromUser(event);
				event.KeyInput.PressedDown = false;
				postEventFromUser(event);
				break;
			case KEYMAP_SPACE:
				event.KeyInput.Key = engine::KEY_SPACE;
				event.EventType = engine::EET_KEY_INPUT_EVENT;
				event.KeyInput.Char = 32;
				event.KeyInput.PressedDown = true;
				postEventFromUser(event);
				event.KeyInput.PressedDown = false;
				postEventFromUser(event);
				break;
			default:
				event.KeyInput.Key = (engine::EKEY_CODE)keycode;
				event.EventType = engine::EET_KEY_INPUT_EVENT;
				event.KeyInput.Char = keycode;
				event.KeyInput.PressedDown = true;
				postEventFromUser(event);
				event.KeyInput.PressedDown = false;
				postEventFromUser(event);
				break;
			}
		if ((pad.Buttons & PSP_CTRL_SQUARE) || (pad.Buttons & PSP_CTRL_TRIANGLE) ||
		    (pad.Buttons & PSP_CTRL_CIRCLE) || (pad.Buttons & PSP_CTRL_CROSS)) keymapPressed = 1;
		else
			keymapPressed = 0;



	} else {

		if (wasStartDown == 1 && (pad.Buttons & PSP_CTRL_START)) return !quit;
		else wasStartDown = 0;

		f32 sensibility = 0.2;
		int fpsc = getVideoDriver()->getFPS();
		if (fpsc == 0) fpsc = 60;

		if (CursorControl->isVisible() == true) sensibility = sensibility = (60/fpsc) *  ((CCursorControl*)CursorControl)->getSpeed();
		// if (CursorControl->isVisible() == true && (pad.Buttons  & PSP_CTRL_RTRIGGER)) sensibility = (60/fpsc) *  0.05;
		bool moved = false;
		const f32 speed = 0.05f;
		core::position2d<s32> CursorPos = CursorControl->getPosition();
		if ((pad.Lx > 160) || (pad.Lx < 110)) {
			f32 xpos = (pad.Lx - 128) * sensibility;
			CursorPos.X += (int)xpos;
			moved=true;
		}
		if ((pad.Ly > 160) || (pad.Ly < 110)) {
			f32 ypos = (pad.Ly - 128) * sensibility;
			CursorPos.Y += (int)ypos;
			moved=true;
		}

		if (CursorPos.Y < 0) CursorPos.Y = 0;
		if (CursorPos.Y > 272) CursorPos.Y = 272;

		if (CursorPos.X < 0) CursorPos.X = 0;
		if  (CursorPos.X > 480) CursorPos.X = 480;
		CursorControl->setPosition(CursorPos);

		if (CursorControl->isEnabled() == true)
		{
			((CCursorControl*)CursorControl)->setPosition2(CursorPos);

		}



		/* Il cursore del mouse è visibile quindi interpreta i pulsanti come mouse */
		if (CursorControl->isVisible() == true && CursorControl->isEnabled() == true)
		{

			if (moved == true)
			{


				event.MouseInput.Event = engine::EMIE_MOUSE_MOVED;
				event.MouseInput.X = CursorPos.X;
				event.MouseInput.Y = CursorPos.Y;
				postEventFromUser(event);


			}

			if (CursorControl->isXEnabled() == true)
			{

				if (wasMouseDown && !(pad.Buttons & PSP_CTRL_CROSS)) {
					event.EventType = engine::EET_MOUSE_INPUT_EVENT;
					event.MouseInput.Event = engine::EMIE_LMOUSE_LEFT_UP;
					event.MouseInput.X = CursorPos.X;
					event.MouseInput.Y = CursorPos.Y;
					postEventFromUser(event);
					wasMouseDown=0;
				}

				if (pad.Buttons & PSP_CTRL_CROSS)
				{
					if (wasMouseDown == 0) {

						event.MouseInput.Event = engine::EMIE_LMOUSE_PRESSED_DOWN;
						event.MouseInput.X = CursorPos.X;
						event.MouseInput.Y = CursorPos.Y;
						postEventFromUser(event);
						wasMouseDown = 1;

					}
				}
			}


		}
		/* Il cursore del mouse non è visibile quindi interpreta i pulsanti
		   come tastiera */


		event.KeyInput.Char = 0;
		event.EventType = engine::EET_KEY_INPUT_EVENT;
		event.KeyInput.PressedDown = true;

		event.KeyInput.Key = engine::KEY_SQUARE;
		if (pad.Buttons & PSP_CTRL_SQUARE){

			event.EventType = engine::EET_KEY_INPUT_EVENT;
			event.KeyInput.PressedDown = true;
			event.KeyInput.PressedOnce = false;



			if (__table_pressed_down[(int)event.KeyInput.Key] == 0)
				__lte_send_po(event.KeyInput.Key);

			__table_pressed_down[(int)event.KeyInput.Key]= 1;
			postEventFromUser(event);
		} else if (__table_pressed_down[(int)event.KeyInput.Key] == 1) __table_pressed_down[(int)event.KeyInput.Key] = -1;


		event.KeyInput.Key = engine::KEY_TRIANGLE;
		if (pad.Buttons & PSP_CTRL_TRIANGLE){
			event.EventType = engine::EET_KEY_INPUT_EVENT;
			event.KeyInput.PressedDown = true;

			if (__table_pressed_down[(int)event.KeyInput.Key] == 0)
				__lte_send_po(event.KeyInput.Key);


			__table_pressed_down[(int)event.KeyInput.Key]= 1;
			postEventFromUser(event);
		} else if (__table_pressed_down[(int)event.KeyInput.Key] == 1) __table_pressed_down[(int)event.KeyInput.Key] = -1;


		event.KeyInput.Key = engine::KEY_CIRCLE;
		if (pad.Buttons & PSP_CTRL_CIRCLE){

			event.EventType = engine::EET_KEY_INPUT_EVENT;
			event.KeyInput.PressedDown = true;


			if (__table_pressed_down[(int)event.KeyInput.Key] == 0)
				__lte_send_po(event.KeyInput.Key);

			__table_pressed_down[(int)event.KeyInput.Key]= 1;
			postEventFromUser(event);
		} else if (__table_pressed_down[(int)event.KeyInput.Key] == 1) __table_pressed_down[(int)event.KeyInput.Key] = -1;


		event.KeyInput.Key = engine::KEY_CROSS;

		if (pad.Buttons & PSP_CTRL_CROSS){
			event.EventType = engine::EET_KEY_INPUT_EVENT;
			event.KeyInput.PressedDown = true;


			if (__table_pressed_down[(int)event.KeyInput.Key] == 0)
				__lte_send_po(event.KeyInput.Key);

			__table_pressed_down[(int)event.KeyInput.Key]= 1;

			postEventFromUser(event);
		} else if (__table_pressed_down[(int)event.KeyInput.Key] == 1) __table_pressed_down[(int)event.KeyInput.Key] = -1;


		event.KeyInput.Key = engine::KEY_UP;
		if (pad.Buttons & PSP_CTRL_UP){
			event.EventType = engine::EET_KEY_INPUT_EVENT;
			event.KeyInput.PressedDown = true;


			if (__table_pressed_down[(int)event.KeyInput.Key] == 0)
				__lte_send_po(event.KeyInput.Key);

			__table_pressed_down[(int)event.KeyInput.Key]= 1;
			postEventFromUser(event);
		} else if (__table_pressed_down[(int)event.KeyInput.Key] == 1) __table_pressed_down[(int)event.KeyInput.Key] = -1;
		event.KeyInput.Key = engine::KEY_DOWN;
		if (pad.Buttons & PSP_CTRL_DOWN){
			event.EventType = engine::EET_KEY_INPUT_EVENT;
			event.KeyInput.PressedDown = true;

			if (__table_pressed_down[(int)event.KeyInput.Key] == 0)
				__lte_send_po(event.KeyInput.Key);

			__table_pressed_down[(int)event.KeyInput.Key]= 1;
			postEventFromUser(event);
		} else if (__table_pressed_down[(int)event.KeyInput.Key] == 1) __table_pressed_down[(int)event.KeyInput.Key] = -1;
		event.KeyInput.Key = engine::KEY_LEFT;
		if (pad.Buttons & PSP_CTRL_LEFT){
			event.EventType = engine::EET_KEY_INPUT_EVENT;
			event.KeyInput.PressedDown = true;

			if (__table_pressed_down[(int)event.KeyInput.Key] == 0)
				__lte_send_po(event.KeyInput.Key);

			__table_pressed_down[(int)event.KeyInput.Key]= 1;
			postEventFromUser(event);
		}  else if (__table_pressed_down[(int)event.KeyInput.Key] == 1) __table_pressed_down[(int)event.KeyInput.Key] = -1;
		event.KeyInput.Key = engine::KEY_RIGHT;
		if (pad.Buttons & PSP_CTRL_RIGHT){
			event.EventType = engine::EET_KEY_INPUT_EVENT;
			event.KeyInput.PressedDown = true;

			if (__table_pressed_down[(int)event.KeyInput.Key] == 0)
				__lte_send_po(event.KeyInput.Key);

			__table_pressed_down[(int)event.KeyInput.Key]= 1;
			postEventFromUser(event);
		} else if (__table_pressed_down[(int)event.KeyInput.Key] == 1) __table_pressed_down[(int)event.KeyInput.Key] = -1;

		event.KeyInput.Key = engine::KEY_START;

		if (pad.Buttons & PSP_CTRL_START){
			event.EventType = engine::EET_KEY_INPUT_EVENT;
			event.KeyInput.PressedDown = true;


			if (__table_pressed_down[(int)event.KeyInput.Key] == 0)
				__lte_send_po(event.KeyInput.Key);

			__table_pressed_down[(int)event.KeyInput.Key]= 1;
			postEventFromUser(event);
		} else if (__table_pressed_down[(int)event.KeyInput.Key] == 1) __table_pressed_down[(int)event.KeyInput.Key] = -1;


		event.KeyInput.Key = engine::KEY_SELECT;
		if (pad.Buttons & PSP_CTRL_SELECT){
			event.EventType = engine::EET_KEY_INPUT_EVENT;
			event.KeyInput.PressedDown = true;

			if (__table_pressed_down[(int)event.KeyInput.Key] == 0)
				__lte_send_po(event.KeyInput.Key);

			__table_pressed_down[(int)event.KeyInput.Key]= 1;
			postEventFromUser(event);
		} else if (__table_pressed_down[(int)event.KeyInput.Key] == 1) __table_pressed_down[(int)event.KeyInput.Key] = -1;
		event.KeyInput.Key = engine::KEY_LBUTTON;
		if (pad.Buttons & PSP_CTRL_LTRIGGER){


			if (__table_pressed_down[(int)event.KeyInput.Key] == 0)
				__lte_send_po(event.KeyInput.Key);
			__table_pressed_down[(int)event.KeyInput.Key]= 1;
			postEventFromUser(event);
		} else if (__table_pressed_down[(int)event.KeyInput.Key] == 1) __table_pressed_down[(int)event.KeyInput.Key] = -1;
		event.KeyInput.Key = engine::KEY_HOME;
		if (pad.Buttons & PSP_CTRL_HOME){
			event.EventType = engine::EET_KEY_INPUT_EVENT;
			event.KeyInput.PressedDown = true;

			if (__table_pressed_down[(int)event.KeyInput.Key] == 0)
				__lte_send_po(event.KeyInput.Key);

			__table_pressed_down[(int)event.KeyInput.Key]= 1;
			postEventFromUser(event);
		} else if (__table_pressed_down[(int)event.KeyInput.Key] == 1) __table_pressed_down[(int)event.KeyInput.Key] = -1;

		event.KeyInput.Key = engine::KEY_NOTE;
		if (pad.Buttons & PSP_CTRL_NOTE){
			event.EventType = engine::EET_KEY_INPUT_EVENT;
			event.KeyInput.PressedDown = true;

			if (__table_pressed_down[(int)event.KeyInput.Key] == 0)
				__lte_send_po(event.KeyInput.Key);

			__table_pressed_down[(int)event.KeyInput.Key]= 1;
			postEventFromUser(event);
		} else if (__table_pressed_down[(int)event.KeyInput.Key] == 1) __table_pressed_down[(int)event.KeyInput.Key] = -1;
		event.KeyInput.Key = engine::KEY_RBUTTON;

		if (pad.Buttons & PSP_CTRL_RTRIGGER ){
			event.EventType = engine::EET_KEY_INPUT_EVENT;
			event.KeyInput.PressedDown = true;

			if (__table_pressed_down[(int)event.KeyInput.Key] == 0)
				__lte_send_po(event.KeyInput.Key);

			__table_pressed_down[(int)event.KeyInput.Key]= 1;
			postEventFromUser(event);
		} else if (__table_pressed_down[(int)event.KeyInput.Key] == 1) __table_pressed_down[(int)event.KeyInput.Key] = -1;

		for (int x = 1; x < 0x19; x++)
		{

			event.KeyInput.Char = 0;
			event.EventType = engine::EET_KEY_INPUT_EVENT;
			event.KeyInput.PressedDown = false;
			event.KeyInput.Key = (engine::EKEY_CODE)x;
			if (__table_pressed_down[(int)event.KeyInput.Key] == -1) {
				__table_pressed_down[(int)event.KeyInput.Key] = 0;
				postEventFromUser(event);



			}

		}

	}





	// sposto il cursore del mouse

	// ------------------------------------------*

	return !quit;
}
void CIrrDeviceWin32::pollJoysticks()
{
#if defined _IRR_COMPILE_WITH_JOYSTICK_EVENTS_
	if(0 == ActiveJoysticks.size())
		return;

	u32 joystick;
	JOYINFOEX info;
	info.dwSize = sizeof(info);
	info.dwFlags = JOY_RETURNALL;

	for(joystick = 0; joystick < ActiveJoysticks.size(); ++joystick)
	{
		if(JOYERR_NOERROR == joyGetPosEx(ActiveJoysticks[joystick].Index, &info))
		{
			SEvent event;
			const JOYCAPS & caps = ActiveJoysticks[joystick].Caps;

			event.EventType = irr::EET_JOYSTICK_INPUT_EVENT;
			event.JoystickEvent.Joystick = (u8)joystick;

			event.JoystickEvent.POV = (u16)info.dwPOV;
			if(event.JoystickEvent.POV > 35900)
				event.JoystickEvent.POV = 65535;

			for(int axis = 0; axis < SEvent::SJoystickEvent::NUMBER_OF_AXES; ++axis)
				event.JoystickEvent.Axis[axis] = 0;

			switch(caps.wNumAxes)
			{
			default:
			case 6:
				event.JoystickEvent.Axis[SEvent::SJoystickEvent::AXIS_V] = 
					(s16)((65535 * (info.dwVpos - caps.wVmin)) / (caps.wVmax - caps.wVmin) - 32768);

			case 5:
				event.JoystickEvent.Axis[SEvent::SJoystickEvent::AXIS_U] = 
					(s16)((65535 * (info.dwUpos - caps.wUmin)) / (caps.wUmax - caps.wUmin) - 32768);

			case 4:
				event.JoystickEvent.Axis[SEvent::SJoystickEvent::AXIS_R] =
					(s16)((65535 * (info.dwRpos - caps.wRmin)) / (caps.wRmax - caps.wRmin) - 32768);

			case 3:
				event.JoystickEvent.Axis[SEvent::SJoystickEvent::AXIS_Z] = 
					(s16)((65535 * (info.dwZpos - caps.wZmin)) / (caps.wZmax - caps.wZmin) - 32768);
			
			case 2:
				event.JoystickEvent.Axis[SEvent::SJoystickEvent::AXIS_Y] =
					(s16)((65535 * (info.dwYpos - caps.wYmin)) / (caps.wYmax - caps.wYmin) - 32768);

			case 1:
				event.JoystickEvent.Axis[SEvent::SJoystickEvent::AXIS_X] = 
					(s16)((65535 * (info.dwXpos - caps.wXmin)) / (caps.wXmax - caps.wXmin) - 32768);
			}
			
			event.JoystickEvent.ButtonStates = info.dwButtons;

			(void)postEventFromUser(event);
		}
	}
#endif // _IRR_COMPILE_WITH_JOYSTICK_EVENTS_
} 
//! runs the device. Returns false if device wants to be deleted
bool CIrrDeviceSDL::run()
{
	os::Timer::tick();

	SEvent irrevent;
	SDL_Event SDL_event;

	while ( !Close && SDL_PollEvent( &SDL_event ) )
	{
		switch ( SDL_event.type )
		{
		case SDL_MOUSEMOTION:
			irrevent.EventType = irr::EET_MOUSE_INPUT_EVENT;
			irrevent.MouseInput.Event = irr::EMIE_MOUSE_MOVED;
			MouseX = irrevent.MouseInput.X = SDL_event.motion.x;
			MouseY = irrevent.MouseInput.Y = SDL_event.motion.y;
			irrevent.MouseInput.ButtonStates = MouseButtonStates;

			postEventFromUser(irrevent);
			break;

		case SDL_MOUSEBUTTONDOWN:
		case SDL_MOUSEBUTTONUP:

			irrevent.EventType = irr::EET_MOUSE_INPUT_EVENT;
			irrevent.MouseInput.X = SDL_event.button.x;
			irrevent.MouseInput.Y = SDL_event.button.y;

			irrevent.MouseInput.Event = irr::EMIE_MOUSE_MOVED;

			switch(SDL_event.button.button)
			{
			case SDL_BUTTON_LEFT:
				if (SDL_event.type == SDL_MOUSEBUTTONDOWN)
				{
					irrevent.MouseInput.Event = irr::EMIE_LMOUSE_PRESSED_DOWN;
					MouseButtonStates |= irr::EMBSM_LEFT;
				}
				else
				{
					irrevent.MouseInput.Event = irr::EMIE_LMOUSE_LEFT_UP;
					MouseButtonStates &= !irr::EMBSM_LEFT;
				}
				break;

			case SDL_BUTTON_RIGHT:
				if (SDL_event.type == SDL_MOUSEBUTTONDOWN)
				{
					irrevent.MouseInput.Event = irr::EMIE_RMOUSE_PRESSED_DOWN;
					MouseButtonStates |= irr::EMBSM_RIGHT;
				}
				else
				{
					irrevent.MouseInput.Event = irr::EMIE_RMOUSE_LEFT_UP;
					MouseButtonStates &= !irr::EMBSM_RIGHT;
				}
				break;

			case SDL_BUTTON_MIDDLE:
				if (SDL_event.type == SDL_MOUSEBUTTONDOWN)
				{
					irrevent.MouseInput.Event = irr::EMIE_MMOUSE_PRESSED_DOWN;
					MouseButtonStates |= irr::EMBSM_MIDDLE;
				}
				else
				{
					irrevent.MouseInput.Event = irr::EMIE_MMOUSE_LEFT_UP;
					MouseButtonStates &= !irr::EMBSM_MIDDLE;
				}
				break;

			case SDL_BUTTON_WHEELUP:
				irrevent.MouseInput.Event = irr::EMIE_MOUSE_WHEEL;
				irrevent.MouseInput.Wheel = 1.0f;
				break;

			case SDL_BUTTON_WHEELDOWN:
				irrevent.MouseInput.Event = irr::EMIE_MOUSE_WHEEL;
				irrevent.MouseInput.Wheel = -1.0f;
				break;
			}

			irrevent.MouseInput.ButtonStates = MouseButtonStates;

			if (irrevent.MouseInput.Event != irr::EMIE_MOUSE_MOVED)
			{
				postEventFromUser(irrevent);

				if ( irrevent.MouseInput.Event >= EMIE_LMOUSE_PRESSED_DOWN && irrevent.MouseInput.Event <= EMIE_MMOUSE_PRESSED_DOWN )
				{
					u32 clicks = checkSuccessiveClicks(irrevent.MouseInput.X, irrevent.MouseInput.Y, irrevent.MouseInput.Event);
					if ( clicks == 2 )
					{
						irrevent.MouseInput.Event = (EMOUSE_INPUT_EVENT)(EMIE_LMOUSE_DOUBLE_CLICK + irrevent.MouseInput.Event-EMIE_LMOUSE_PRESSED_DOWN);
						postEventFromUser(irrevent);
					}
					else if ( clicks == 3 )
					{
						irrevent.MouseInput.Event = (EMOUSE_INPUT_EVENT)(EMIE_LMOUSE_TRIPLE_CLICK + irrevent.MouseInput.Event-EMIE_LMOUSE_PRESSED_DOWN);
						postEventFromUser(irrevent);
					}
				}
			}
			break;

		case SDL_KEYDOWN:
		case SDL_KEYUP:
			{
				SKeyMap mp;
				mp.SDLKey = SDL_event.key.keysym.sym;
				s32 idx = KeyMap.binary_search(mp);

				EKEY_CODE key;
				if (idx == -1)
					key = (EKEY_CODE)0;
				else
					key = (EKEY_CODE)KeyMap[idx].Win32Key;

#ifdef _IRR_WINDOWS_API_
				// handle alt+f4 in Windows, because SDL seems not to
				if ( (SDL_event.key.keysym.mod & KMOD_LALT) && key == KEY_F4)
				{
					Close = true;
					break;
				}
#endif
				irrevent.EventType = irr::EET_KEY_INPUT_EVENT;
				irrevent.KeyInput.Char = SDL_event.key.keysym.unicode;
				irrevent.KeyInput.Key = key;
				irrevent.KeyInput.PressedDown = (SDL_event.type == SDL_KEYDOWN);
				irrevent.KeyInput.Shift = (SDL_event.key.keysym.mod & KMOD_SHIFT) != 0;
				irrevent.KeyInput.Control = (SDL_event.key.keysym.mod & KMOD_CTRL ) != 0;
				postEventFromUser(irrevent);
			}
			break;

		case SDL_QUIT:
			Close = true;
			break;

		case SDL_ACTIVEEVENT:
			if ((SDL_event.active.state == SDL_APPMOUSEFOCUS) ||
					(SDL_event.active.state == SDL_APPINPUTFOCUS))
				WindowHasFocus = (SDL_event.active.gain==1);
			else
			if (SDL_event.active.state == SDL_APPACTIVE)
				WindowMinimized = (SDL_event.active.gain!=1);
			break;

		case SDL_VIDEORESIZE:
			if ((SDL_event.resize.w != (int)Width) || (SDL_event.resize.h != (int)Height))
			{
				Width = SDL_event.resize.w;
				Height = SDL_event.resize.h;
				Screen = SDL_SetVideoMode( Width, Height, 0, SDL_Flags );
				if (VideoDriver)
					VideoDriver->OnResize(core::dimension2d<u32>(Width, Height));
			}
			break;

		case SDL_USEREVENT:
			irrevent.EventType = irr::EET_USER_EVENT;
			irrevent.UserEvent.UserData1 = *(reinterpret_cast<s32*>(&SDL_event.user.data1));
			irrevent.UserEvent.UserData2 = *(reinterpret_cast<s32*>(&SDL_event.user.data2));

			postEventFromUser(irrevent);
			break;

		default:
			break;
		} // end switch

	} // end while

#if defined(_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)
	// TODO: Check if the multiple open/close calls are too expensive, then
	// open/close in the constructor/destructor instead

	// update joystick states manually
	SDL_JoystickUpdate();
	// we'll always send joystick input events...
	SEvent joyevent;
	joyevent.EventType = EET_JOYSTICK_INPUT_EVENT;
	for (u32 i=0; i<Joysticks.size(); ++i)
	{
		SDL_Joystick* joystick = Joysticks[i];
		if (joystick)
		{
			int j;
			// query all buttons
			const int numButtons = core::min_(SDL_JoystickNumButtons(joystick), 32);
			joyevent.JoystickEvent.ButtonStates=0;
			for (j=0; j<numButtons; ++j)
				joyevent.JoystickEvent.ButtonStates |= (SDL_JoystickGetButton(joystick, j)<<j);

			// query all axes, already in correct range
			const int numAxes = core::min_(SDL_JoystickNumAxes(joystick), SEvent::SJoystickEvent::NUMBER_OF_AXES);
			joyevent.JoystickEvent.Axis[SEvent::SJoystickEvent::AXIS_X]=0;
			joyevent.JoystickEvent.Axis[SEvent::SJoystickEvent::AXIS_Y]=0;
			joyevent.JoystickEvent.Axis[SEvent::SJoystickEvent::AXIS_Z]=0;
			joyevent.JoystickEvent.Axis[SEvent::SJoystickEvent::AXIS_R]=0;
			joyevent.JoystickEvent.Axis[SEvent::SJoystickEvent::AXIS_U]=0;
			joyevent.JoystickEvent.Axis[SEvent::SJoystickEvent::AXIS_V]=0;
			for (j=0; j<numAxes; ++j)
				joyevent.JoystickEvent.Axis[j] = SDL_JoystickGetAxis(joystick, j);

			// we can only query one hat, SDL only supports 8 directions
			if (SDL_JoystickNumHats(joystick)>0)
			{
				switch (SDL_JoystickGetHat(joystick, 0))
				{
					case SDL_HAT_UP:
						joyevent.JoystickEvent.POV=0;
						break;
					case SDL_HAT_RIGHTUP:
						joyevent.JoystickEvent.POV=4500;
						break;
					case SDL_HAT_RIGHT:
						joyevent.JoystickEvent.POV=9000;
						break;
					case SDL_HAT_RIGHTDOWN:
						joyevent.JoystickEvent.POV=13500;
						break;
					case SDL_HAT_DOWN:
						joyevent.JoystickEvent.POV=18000;
						break;
					case SDL_HAT_LEFTDOWN:
						joyevent.JoystickEvent.POV=22500;
						break;
					case SDL_HAT_LEFT:
						joyevent.JoystickEvent.POV=27000;
						break;
					case SDL_HAT_LEFTUP:
						joyevent.JoystickEvent.POV=31500;
						break;
					case SDL_HAT_CENTERED:
					default:
						joyevent.JoystickEvent.POV=65535;
						break;
				}
			}
			else
			{
				joyevent.JoystickEvent.POV=65535;
			}

			// we map the number directly
			joyevent.JoystickEvent.Joystick=static_cast<u8>(i);
			// now post the event
			postEventFromUser(joyevent);
			// and close the joystick
		}
	}
#endif
	return !Close;
}