May 07, 2024, 07:18:41 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Mouse Input in Console Applications.

Started by WayneA, April 09, 2009, 09:22:32 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

WayneA

This is something I've wanted to do some for sometime and about 3-4 years ago when I initially tried to figure it out I did not get very far. I just decided to try again this morning and it was fairly simple. The UDT for INPUT_RECORD was a little complicated and added about 30 minutes of research but all in all this only took about 45-60 minutes to crank out. I'm pretty happy with this. I hope you all enjoy it!

$Include "windows.inc"
AutoDefine "Off"
Type Input_Record,1
Dim EventType As Word
Dim two_byte_alignment[2] As Char
Union
Dim KeyEvent As KEY_EVENT_RECORD
Dim MouseEvent As MOUSE_EVENT_RECORD
Dim WindowBufferSizeEvent As WINDOW_BUFFER_SIZE_RECORD
Dim MenuEvent As MENU_EVENT_RECORD
Dim FocusEvent As FOCUS_EVENT_RECORD
EndUnion
EndType
Declare Import,ReadConsoleInputA(hConsoleInput As UInt,lpBuffer As Input_Record,nLength As UInt,lpNumberOfEventsRead As UInt ByRef),Int
OpenConsole
Dim input_buffer As Input_Record
Dim input_read,hConsole,Quit=False As Int
hConsole=_GetStdHandle(STD_INPUT_HANDLE)
If hConsole<>INVALID_HANDLE_VALUE Then
If _SetConsoleMode(hConsole,ENABLE_MOUSE_INPUT) Then
Do
If ReadConsoleInputA(hConsole,input_buffer,1,input_read) Then
If input_read>0 Then
Select input_buffer.EventType
Case KEY_EVENT
Case MOUSE_EVENTC
_SetConsoleTitle(str$(input_buffer.MouseEvent.dwMousePosition.Y)+str$(input_buffer.MouseEvent.dwMousePosition.X))
If input_buffer.MouseEvent.dwButtonState=FROM_LEFT_1ST_BUTTON_PRESSED
Locate input_buffer.MouseEvent.dwMousePosition.Y+1,input_buffer.MouseEvent.dwMousePosition.X+1
Print "@",
ElseIf input_buffer.MouseEvent.dwButtonState=RIGHTMOST_BUTTON_PRESSED
Quit=True
EndIf
Case WINDOW_BUFFER_SIZE_EVENT
Case MENU_EVENT
Case FOCUS_EVENT
EndSelect
EndIf
EndIf
_Sleep(1)
Until Quit
Locate 0,0
Print "Press Any Key to Quit...",
Do:Until Inkey$<>""
End
EndIf
EndIf
99 little bugs in the code,
99 bugs in the code,
Fix one bug,
Compile again,
104 little bugs in the code...

All code I post is in the public domain.

Haim