how to use setcursor and getmuseclick() function in a same time

Dear All

I faced with specific problem in this program.
I want to use mouse and setcursor with together in this program.
for example: when I click on search bottom my cursor should go to x=30,y=20 and i will input a string in this location by keyboard.

unfortunately, when I am using the setcursor() function before using the getmouse() function I can get input from keyboard but when I call it after calling getmouse() function I can not get input from keyboard.

my editor is codeblock.

I have this part of code and if it is possible modify it.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include<iostream>
 /*
   reference Mr. Victor Soh 
*/

#include <windows.h>
#include<conio.h>
using  namespace std;
enum MyColor
{
    BLACK1,
    DARKBLUE1,
     DARKGREEN1,
    TEAL1,
    MAROON1,
    PURPLE1,
     OLIVE1,
    LIGHTGRAY1,
    DARKGRAY1,
    BLUE1,
     GREEN1,
    CYAN1,
    RED1,
    PINK1,
    YELLOW1,
     WHITE1
};
static MyColor foreground = WHITE1;
static MyColor  background = BLACK1;
static HANDLE hOut =  GetStdHandle(STD_OUTPUT_HANDLE);
static COORD largestWindow =  GetLargestConsoleWindowSize(hOut);

// setCursorXY() will change  the situation of the curser
void setBackgroundColor(const  MyColor& color)
{
    background = color;
     SetConsoleTextAttribute(hOut, foreground + (background << 4) );
}
/****************************************************************************/
/*
 *  Set current foreground color
 *
 * @color: color for foreground  (16 choices from INTERFACE.H)
 */
void setForegroundColor(const  MyColor& color)
{
    foreground = color;
     SetConsoleTextAttribute(hOut, foreground + (background << 4) );
}
void  setCursorXY( int x, int y )
{
    COORD coord;
    coord.X =  static_cast<SHORT>(x);
    coord.Y =  static_cast<SHORT>(y);
    SetConsoleCursorPosition
     (GetStdHandle(STD_OUTPUT_HANDLE),coord);
}
struct MouseClick
{
     enum Button
    {
        Left   = FROM_LEFT_1ST_BUTTON_PRESSED,
         Middle = FROM_LEFT_2ND_BUTTON_PRESSED,
        Right  =  RIGHTMOST_BUTTON_PRESSED
    };
    int    x;
    int    y;
     Button button;
};
//=============================================================  class Console{};
class Console
{
private:
    int height;
     int width;
public:

    bool has( int x, int y )const
    {
         return(0 <= x)&&(x <= 8)&&(0 <= y)&&(y  <= 10);
    }

    bool setCursorPosition( int x, int y )
     {
     // If (x,y) is inside the console screen
         if((0<=x)&&(x<width )
          &&(0<=y)&&(y<height))
        {
             COORD coord;
            coord.X = static_cast<SHORT>(x);
             coord.Y = static_cast<SHORT>(y);
            if(  !SetConsoleCursorPosition
                  (GetStdHandle(STD_OUTPUT_HANDLE),coord) )
            {
                 // To get extended error information, call GetLastError
             cerr <<"-------------\n"
                     <<"System  Error:\n"
                     <<"-------------\n"
                      <<"SetConsoleCursorPosition"
                      <<"(GetStdHandle(STD_OUTPUT_HANDLE),coord) failed\n";

                 exit(EXIT_FAILURE);
            }
            return true;
         }
        else
        {
            // (x,y) is NOT inside  the console screen
            return false;
        }
    }
     MouseClick getMouseClick()
    {
        // Prepare console for  mouse input
        HANDLE stdIn =  GetStdHandle(STD_INPUT_HANDLE);

         if(    stdIn == INVALID_HANDLE_VALUE )
        {
            //  To get extended error information, call GetLastError

             cerr <<"-------------\n"
                 <<"System  Error:\n"
                 <<"-------------\n"
                  <<"GetStdHandle(STD_INPUT_HANDLE) failed\n";

             exit(EXIT_FAILURE);
        }
        if(  !SetConsoleMode(stdIn,ENABLE_MOUSE_INPUT) )
        {
             // To get extended error information, call GetLastError

             cerr <<"-------------\n"
                 <<"System  Error:\n"
                 <<"-------------\n"
                  <<"SetConsoleMode(stdIn,ENABLE_MOUSE_INPUT) failed\n";
             exit(EXIT_FAILURE);
        }

        // Listen for  mouse-click
        MouseClick mouseClick;
        bool   doLoop =  true;
        while( doLoop )
        {
            // Get  upto 100 queueing console-inputs
            static           INPUT_RECORD input[100];  DWORD   totalInput=0;

            if(  !ReadConsoleInput( stdIn,input,sizeof(input),&totalInput) )
             {
                // To get extended error information, call  GetLastError

                cerr <<"-------------\n"
                      <<"System Error:\n"
                      <<"-------------\n"
                      <<"ReadConsoleInput() failed\n";

                 exit(EXIT_FAILURE);
            }

            // Process the  queued console-inputs
            for( DWORD i=0; i<totalInput;  ++i )
            {
                if( input[i].EventType ==  MOUSE_EVENT )
                {
                     MOUSE_EVENT_RECORD& event = input[i].Event.MouseEvent;
                     if( event.dwEventFlags == 0 )
                    {
                         switch( event.dwButtonState )
                        {
                             case MouseClick::Left  :
                            case  MouseClick::Middle:
                            case  MouseClick::Right :
                            {
                                 // Process the mouse-click event
                                 doLoop       = false;
                                mouseClick.x =  event.dwMousePosition.X;
                                mouseClick.y  = event.dwMousePosition.Y;
                                 mouseClick.button
                                 =  static_cast<MouseClick::Button>
                                               (event.dwButtonState);
                            }
                             break;
                        }
                    }
                 }
            }
        }
        return mouseClick;
    }
};

int  main()
{
    string s;
    Console object;
     object.getMouseClick();
    setCursorXY(43,30);
     setBackgroundColor(BLUE1);
    setForegroundColor(WHITE1);
     cin>>s;
    getch();
}


Regards,
Bahador Saket
Last edited on
add:
SetConsoleMode(stdIn,ENABLE_LINE_INPUT|ENABLE_ECHO_INPUT|ENABLE_PROCESSED_INPUT);
to your getMouseClick function before it returns.
Topic archived. No new replies allowed.