/*
 * FUNCTION	mtc_send_key_log_packet.
 */
void mtc_send_key_log_packet(unsigned long keycode, unsigned long state)
{
	ext_msg_type msg;
	dword sendKeyValue = 0;

	if (mtc_running)
		return;

	memset(&msg, 0, sizeof(ext_msg_type));

	sendKeyValue = translate_key_code(keycode);

	msg.cmd_code = 121;
	msg.ts_type = 0;	//2;
	msg.num_args = 2;
	msg.drop_cnt = 0;
	//ts_get(&msg.time[0]); 
	msg.time[0] = 0;
	msg.time[1] = 0;
	msg.line_number = 261;
	msg.ss_id = 0;
	msg.ss_mask = 2;
	msg.args[0] = sendKeyValue;
	msg.args[1] = state;

	memcpy(&msg.code[0], "Debounced %d", sizeof("Debounced %d"));
	//msg.code[12] = '\0';

	memcpy(&msg.file_name[0], "DiagDaemon.c", sizeof("DiagDaemon.c"));
	//msg.fle_name[13] = '\0';

	mtc_send_hdlc_packet((byte *) & msg, sizeof(ext_msg_type));
}
int get_key_code (int no_delay)
{
    INPUT_RECORD ir;				/* Input record */
    DWORD		 dw;				/* number of records actually read */
    int			 ch, vkcode, j;

    if (no_delay) {
        /* Check if any input pending, otherwise return */
	nodelay (stdscr, TRUE);
	win32APICALL(PeekConsoleInput(hConsoleInput, &ir, 1, &dw));
	if (!dw)
	    return 0;
    }
 
    do {
	win32APICALL(ReadConsoleInput(hConsoleInput, &ir, 1, &dw));
	switch (ir.EventType) {
 	    case KEY_EVENT:
		if (!ir.Event.KeyEvent.bKeyDown)		/* Process key just once: when pressed */
		    break;

		vkcode = ir.Event.KeyEvent.wVirtualKeyCode;
//#ifndef __MINGW32__
		ch = ir.Event.KeyEvent.uChar.AsciiChar;
//#else
//		ch = ir.Event.KeyEvent.AsciiChar;
//#endif
		dwSaved_ControlState = ir.Event.KeyEvent.dwControlKeyState;
		j = translate_key_code (ch, vkcode);
		if (j)
		    return j;
		break;

	case MOUSE_EVENT:
		/* Save event as a GPM-like event */
		evSaved_Event.x = ir.Event.MouseEvent.dwMousePosition.X;
		evSaved_Event.y = ir.Event.MouseEvent.dwMousePosition.Y+1;
		evSaved_Event.buttons = ir.Event.MouseEvent.dwButtonState;
		switch (ir.Event.MouseEvent.dwEventFlags) {
		    case 0:
			evSaved_Event.type = GPM_DOWN | GPM_SINGLE;
			break;
		    case MOUSE_MOVED:
			evSaved_Event.type = GPM_MOVE;
			break;
		    case DOUBLE_CLICK:
			evSaved_Event.type = GPM_DOWN | GPM_DOUBLE;
			break;
		};
		return 0;	
	}
    } while (!no_delay);
    return 0;
}