#Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Change2CUI=y #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** ;funkey 2014.10.03 #include #include #include #include #Region EventTypes Global Const $FOCUS_EVENT = 0x10 Global Const $KEY_EVENT = 0x01 Global Const $MENU_EVENT = 0x08 Global Const $MOUSE_EVENT = 0x02 Global Const $WINDOW_BUFFER_SIZE_EVENT = 0x04 #EndRegion EventTypes #Region EventRecords Global Const $tagFOCUS_EVENT_RECORD = "BOOL bSetFocus;DWORD dwPadding[3];" Global Const $tagKEY_EVENT_RECORD = "BOOL bKeyDown;WORD wRepeatCount;WORD wVirtualKeyCode;WORD wVirtualScanCode;WCHAR UnicodeChar;DWORD dwControlKeyState;" Global Const $tagMENU_EVENT_RECORD = "UINT dwCommandId;DWORD dwPadding[3];" Global Const $tagMOUSE_EVENT_RECORD = "SHORT X;SHORT Y;DWORD dwButtonState;DWORD dwControlKeyState;DWORD dwEventFlags;" Global Const $tagWINDOW_BUFFER_SIZE_RECORD = "SHORT X;SHORT Y;DWORD dwButtonState;DWORD dwControlKeyState;DWORD dwEventFlags;" Global Const $tagINPUT_RECORD_FOCUS = "WORD EventType;struct;" & $tagFOCUS_EVENT_RECORD & "endstruct;" Global Const $tagINPUT_RECORD_KEY = "WORD EventType;struct;" & $tagKEY_EVENT_RECORD & "endstruct;" Global Const $tagINPUT_RECORD_MENU = "WORD EventType;struct;" & $tagMENU_EVENT_RECORD & "endstruct;" Global Const $tagINPUT_RECORD_MOUSE = "WORD EventType;struct;" & $tagMOUSE_EVENT_RECORD & "endstruct;" Global Const $tagINPUT_RECORD_WINDOW_BUFFER_SIZE = "WORD EventType;struct;" & $tagWINDOW_BUFFER_SIZE_RECORD & "endstruct;" Global Const $tagINPUT_RECORD = "WORD EventType;DWORD dwDummy[4]" #EndRegion EventRecords #Region Console modes Global Const $ENABLE_ECHO_INPUT = 0x04 Global Const $ENABLE_INSERT_MODE = 0x20 Global Const $ENABLE_LINE_INPUT = 0x02 Global Const $ENABLE_MOUSE_INPUT = 0x10 Global Const $ENABLE_PROCESSED_INPUT = 0x01 Global Const $ENABLE_QUICK_EDIT_MODE = 0x40 Global Const $ENABLE_WINDOW_INPUT = 0x08 Global Const $ENABLE_PROCESSED_OUTPUT = 0x01 Global Const $ENABLE_WRAP_AT_EOL_OUTPUT = 0x02 #EndRegion Console modes #Region Mouse events Global Const $FROM_LEFT_1ST_BUTTON_PRESSED = 0x01 Global Const $FROM_LEFT_2ND_BUTTON_PRESSED = 0x04 Global Const $FROM_LEFT_3RD_BUTTON_PRESSED = 0x08 Global Const $FROM_LEFT_4TH_BUTTON_PRESSED = 0x10 Global Const $RIGHTMOST_BUTTON_PRESSED = 0x02 Global Const $DOUBLE_CLICK = 0x02 Global Const $MOUSE_HWHEELED = 0x08 Global Const $MOUSE_MOVED = 0x01 Global Const $MOUSE_WHEELED = 0x04 #EndRegion Mouse events Global Const $STD_INPUT_HANDLE = -10 Global Const $STD_OUTPUT_HANDLE = -11 Global Const $STD_ERROR_HANDLE = -12 If Not @Compiled Then Exit MsgBox(16, "Error", "This demo script only works when compiled") Opt("GUIOnEventMode", 1) Global $hGui = GUICreate("Console Info Window", 300, 100, @DesktopWidth - 300, 0) GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit") GUICtrlCreateLabel("Mouse position in console:", 10, 10, 150, 20, $SS_CENTERIMAGE) Global $nPosition = GUICtrlCreateInput("", 160, 10, 70, 20, $ES_READONLY) GUISetState() _FreeConsole() If Not _AllocConsole() Then ConsoleWrite("Error AllocConsole input" & @CRLF) Sleep(1000) Exit EndIf Global $hStdIn = _GetStdHandle($STD_INPUT_HANDLE) If $hStdIn = $INVALID_HANDLE_VALUE Then ConsoleWrite("Error GetStdHandle input" & @CRLF) Sleep(1000) Exit EndIf Global $hStdOut = _GetStdHandle($STD_OUTPUT_HANDLE) If $hStdOut = $INVALID_HANDLE_VALUE Then ConsoleWrite("Error GetStdHandle output" & @CRLF) Sleep(1000) Exit EndIf Global $hModeOld = _GetConsoleMode($hStdIn) If @error Then _ConsoleWrite($hStdOut, "Error GetConsoleMode" & @CRLF) Sleep(1000) Exit EndIf If Not _SetConsoleMode($hStdIn, BitOR($ENABLE_WINDOW_INPUT, $ENABLE_MOUSE_INPUT)) Then _ConsoleWrite($hStdOut, "Error SetConsoleMode" & @CRLF) Sleep(1000) Exit EndIf Global $tRecord = DllStructCreate($tagINPUT_RECORD) Global $iRecordsRead _ResizeConsoleBuffer(150, 30) While True If _GetNumberOfConsoleInputEvents($hStdIn) = 0 Then ContinueLoop Sleep(10) EndIf If Not _ReadConsoleInput($hStdIn, $tRecord, 1, $iRecordsRead) Then _ConsoleWrite($hStdOut, "Error ReadConsoleInput" & @CRLF) _Exit() EndIf Switch DllStructGetData($tRecord, "EventType") Case $FOCUS_EVENT ;do nothing Case $KEY_EVENT If Not _KeyEventProc($tRecord) Then ExitLoop Case $MENU_EVENT ;do nothing Case $MOUSE_EVENT _MouseEventProc($tRecord) Case $WINDOW_BUFFER_SIZE_EVENT _ResizeEventProc($tRecord) Case Else _ConsoleWrite($hStdOut, "Error unknown event type" & @CRLF) _Exit() EndSwitch WEnd Func _ResizeConsoleBuffer($w, $h) _SetConsoleScreenBufferSize($hStdOut, $w, $h) If @error Then _ConsoleWrite($hStdOut, "Error SetConsoleScreenBufferSize" & @CRLF) EndIf _SetConsoleWindowInfo($hStdOut, $w - 1, $h - 1) If @error Then _ConsoleWrite($hStdOut, "Error SetConsoleWindowInfo" & @CRLF) EndIf EndFunc ;==>_ResizeConsoleBuffer Func _Exit() _SetConsoleMode($hStdIn, $hModeOld) Exit EndFunc ;==>_Exit Func _FreeConsole() Local $aRet = DllCall("kernel32.dll", "BOOL", "FreeConsole") If @error Or $aRet[0] = 0 Then Return SetError(1, 0, 0) Return $aRet[0] EndFunc ;==>_FreeConsole Func _AllocConsole() Local $aRet = DllCall("kernel32.dll", "BOOL", "AllocConsole") If @error Or $aRet[0] = 0 Then Return SetError(1, 0, 0) Return $aRet[0] EndFunc ;==>_AllocConsole Func _FlushConsoleInputBuffer($hConsole) Local $aRet = DllCall("kernel32.dll", "BOOL", "FlushConsoleInputBuffer", "handle", $hConsole) If @error Or $aRet[0] = 0 Then Return SetError(1, 0, 0) Return $aRet[0] EndFunc ;==>_FlushConsoleInputBuffer Func _GetStdHandle($nStdHandle) Local $aRet = DllCall("kernel32.dll", "handle", "GetStdHandle", "DWORD", $nStdHandle) If @error Or $aRet[0] = 0 Then Return SetError(1, 0, 0) Return $aRet[0] EndFunc ;==>_GetStdHandle Func _GetConsoleMode($hConsole) Local $aRet = DllCall("kernel32.dll", "BOOL", "GetConsoleMode", "handle", $hConsole, "DWORD*", 0) If @error Or $aRet[0] = 0 Then Return SetError(1, 0, 0) Return $aRet[2] EndFunc ;==>_GetConsoleMode Func _SetConsoleMode($hConsole, $dwMode) Local $aRet = DllCall("kernel32.dll", "BOOL", "SetConsoleMode", "handle", $hConsole, "DWORD", $dwMode) If @error Or $aRet[0] = 0 Then Return SetError(1, 0, 0) Return $aRet[0] EndFunc ;==>_SetConsoleMode Func _GetNumberOfConsoleInputEvents($hConsole) Local $aRet = DllCall("kernel32.dll", "BOOL", "GetNumberOfConsoleInputEvents", "handle", $hConsole, "DWORD*", 0) If @error Or $aRet[0] = 0 Then Return SetError(1, 0, 0) Return $aRet[2] EndFunc ;==>_GetNumberOfConsoleInputEvents Func _ReadConsoleInput($hConsole, ByRef $tRecord, $iRecordCount, ByRef $iRecordsRead) Local $aRet = DllCall("kernel32.dll", "BOOL", "ReadConsoleInput", "handle", $hConsole, "struct*", $tRecord, "DWORD", $iRecordCount, "DWORD*", 0) If @error Or $aRet[0] = 0 Then Return SetError(1, 0, 0) $iRecordsRead = $aRet[4] Return $aRet[0] EndFunc ;==>_ReadConsoleInput Func _PeekConsoleInput($hConsole, ByRef $tRecord, $iRecordCount, ByRef $iRecordsRead) Local $aRet = DllCall("kernel32.dll", "BOOL", "PeekConsoleInput", "handle", $hConsole, "struct*", $tRecord, "DWORD", $iRecordCount, "DWORD*", 0) If @error Or $aRet[0] = 0 Then Return SetError(1, 0, 0) $iRecordsRead = $aRet[4] Return $aRet[0] EndFunc ;==>_PeekConsoleInput Func _ConsoleWrite($hConsoleOut, $sText) Local $aRet = DllCall("kernel32.dll", "BOOL", "WriteConsole", "handle", $hConsoleOut, "str", $sText, "DWORD", StringLen($sText), "DWORD*", 0, "ptr", 0) EndFunc ;==>_ConsoleWrite Func _SetConsoleWindowInfo($hConsoleOut, $w, $h) Local $tSmallRect = DllStructCreate("SHORT Left;SHORT Top;SHORT Right;SHORT Bottom;") DllStructSetData($tSmallRect, "Right", $w) DllStructSetData($tSmallRect, "Bottom", $h) Local $aRet = DllCall("kernel32.dll", "BOOL", "SetConsoleWindowInfo", "handle", $hConsoleOut, "BOOL", True, "struct*", $tSmallRect) If @error Or $aRet[0] = 0 Then Return SetError(1, 0, 0) Return $aRet[0] EndFunc ;==>_SetConsoleWindowInfo Func _SetConsoleScreenBufferSize($hConsoleOut, $w, $h) Local $tCoord = DllStructCreate("SHORT X;SHORT Y;") DllStructSetData($tCoord, "X", $w) DllStructSetData($tCoord, "Y", $h) Local $tDword = DllStructCreate("DWORD Coord", DllStructGetPtr($tCoord)) Local $aRet = DllCall("kernel32.dll", "BOOL", "SetConsoleScreenBufferSize", "handle", $hConsoleOut, "DWORD", DllStructGetData($tDword, "Coord")) If @error Or $aRet[0] = 0 Then Return SetError(1, 0, 0) Return $aRet[0] EndFunc ;==>_SetConsoleScreenBufferSize Func _KeyEventProc($tRecord) Local $tKeyRecord = DllStructCreate($tagINPUT_RECORD_KEY, DllStructGetPtr($tRecord)) Local $bPressed = DllStructGetData($tKeyRecord, "bKeyDown") Local $cChar = DllStructGetData($tKeyRecord, "UnicodeChar") If $cChar = "" Then Return 1 ;don't show modifier keys (ctrl, alt, shift, pause, F1, F2, ...) If $bPressed Then _ConsoleWrite($hStdOut, "Key pressed: " & $cChar & @CRLF) Else _ConsoleWrite($hStdOut, "Key released: " & $cChar & @CRLF) EndIf If Asc($cChar) = 27 Then Return 0 ; end script when 'Escape' is pressed Return 1 EndFunc ;==>_KeyEventProc Func _MouseEventProc($tRecord) ConsoleWrite("Mouse event: ") Local $tMouseRecord = DllStructCreate($tagINPUT_RECORD_MOUSE, DllStructGetPtr($tRecord)) Local $iMouseEvent = DllStructGetData($tMouseRecord, "dwEventFlags") Local $iButtonState = DllStructGetData($tMouseRecord, "dwButtonState") Local $x = DllStructGetData($tMouseRecord, "X") Local $y = DllStructGetData($tMouseRecord, "Y") GUICtrlSetData($nPosition, StringFormat("%d,%d", $x, $y)) _ConsoleWrite($hStdOut, StringFormat("Position: [%03d,%03d] - ", $x, $y)) Switch $iMouseEvent Case 0 If $iButtonState = $FROM_LEFT_1ST_BUTTON_PRESSED Then _ConsoleWrite($hStdOut, "Left button pressed" & @CRLF) ElseIf $iButtonState = $RIGHTMOST_BUTTON_PRESSED Then _ConsoleWrite($hStdOut, "Right button pressed" & @CRLF) Else _ConsoleWrite($hStdOut, "Button pressed" & @CRLF) EndIf Case $DOUBLE_CLICK _ConsoleWrite($hStdOut, "Double clicked" & @CRLF) Case $MOUSE_HWHEELED _ConsoleWrite($hStdOut, "Horizontal wheeled" & @CRLF) Case $MOUSE_MOVED _ConsoleWrite($hStdOut, "Mouse moved" & @CRLF) Case $MOUSE_WHEELED _ConsoleWrite($hStdOut, "Vertical wheeled" & @CRLF) Case Else _ConsoleWrite($hStdOut, "Unknown mouse action" & @CRLF) EndSwitch EndFunc ;==>_MouseEventProc Func _ResizeEventProc($tRecord) Local $tResizeRecord = DllStructCreate($tagINPUT_RECORD_WINDOW_BUFFER_SIZE, DllStructGetPtr($tRecord)) Local $iColumns = DllStructGetData($tResizeRecord, "X") Local $iRows = DllStructGetData($tResizeRecord, "Y") _ConsoleWrite($hStdOut, "Console screen buffer is now " & $iColumns & " columns by " & $iRows & " rows" & @CRLF) EndFunc ;==>_ResizeEventProc