char *GetInput()
{
char *Buffer;
static char buf[100];
Buffer = buf;
FlushConsoleInputBuffer(hInput);
ReadConsoleInput(hInput, &record, 1, &cRead);
record.EventType = NULL;
while (record.Event.KeyEvent.wVirtualKeyCode != VK_RETURN)
{
//wait until a key is pressed
while (record.EventType != KEY_EVENT)
ReadConsoleInput(hInput, &record, 1, &cRead);
if(isalnum(record.Event.KeyEvent.uChar.AsciiChar))
{
Buffer = strcat(Buffer, &record.Event.KeyEvent.uChar.AsciiChar);
GetConsoleScreenBufferInfo(hOutput, &ScreenBuffer);
WriteConsoleOutputCharacter(
hOutput, // screen buffer handle
&record.Event.KeyEvent.uChar.AsciiChar, // pointer to source string
1, // length of string
ScreenBuffer.dwCursorPosition, // first cell to write to
&cWritten);
CursorPos.X = ScreenBuffer.dwCursorPosition.X + 1;
CursorPos.Y = ScreenBuffer.dwCursorPosition.Y;
SetConsoleCursorPosition(hOutput, CursorPos);
}
record.EventType = NULL;
}
return(Buffer);
}
record, hInput, CursorPos and hOutput are defined globaly.
Any ideas???
"Mr Sandman bring me a dream"
Edited by - MrSandman666 on 10/13/00 7:46:08 AM
Win32 console input
Ok. I use the Win32 Console for my text based game.
I need to get Input from the user but I didn't want to use cin>> so I decided to write my own input function.
It works well, except for the fact that it displays every Character doubled!
Example:
I type this: Hello
It reads: HHeelloo
Here's my function:
-----------------------------"Mr Sandman bring me a dream"
weeell, my guess would be that the system auotmatically prints a character when you press a key. you are doing the same, so everything is doubled
i *think* there is an API functions to disable this. look up something in the group where "GetStdHandle" belongs to, perhaps you will find it
rid
i *think* there is an API functions to disable this. look up something in the group where "GetStdHandle" belongs to, perhaps you will find it
rid
Nope, that''s not it.
The ReadConsoleInput function doesn''t echoe the input.
besides, I have to move the curor manualy in order to get a string and not overwriting the old character over and over again.
And the cursor moves twice, even when I comment out the WriteConsoleOutputCharacter function. For some reason the loop loops twice. And I can''t tell you why. As this function deals with keyboard input, it''s almost impossible to step through it.
By the way, when place a breakpoint at the SetCursorPos line, and let the programm run again when I reach it ( no stepping, just a break) it works perfectly.
And I also experienced the following:
When I release the shift key fast enough, I can get something like the "Hheelloo" with only one capital letter instead of two. I suspect the error to be something about the buffer, but I can''t tell what it is exactly.
"Mr Sandman bring me a dream"
The ReadConsoleInput function doesn''t echoe the input.
besides, I have to move the curor manualy in order to get a string and not overwriting the old character over and over again.
And the cursor moves twice, even when I comment out the WriteConsoleOutputCharacter function. For some reason the loop loops twice. And I can''t tell you why. As this function deals with keyboard input, it''s almost impossible to step through it.
By the way, when place a breakpoint at the SetCursorPos line, and let the programm run again when I reach it ( no stepping, just a break) it works perfectly.
And I also experienced the following:
When I release the shift key fast enough, I can get something like the "Hheelloo" with only one capital letter instead of two. I suspect the error to be something about the buffer, but I can''t tell what it is exactly.
"Mr Sandman bring me a dream"
-----------------------------"Mr Sandman bring me a dream"
why not use cin ?
you are re-inventing the wheel basically.
"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions
you are re-inventing the wheel basically.
"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions
I know that I''m reinventing the wheel but I have my reasons.
I want to be able to make the input echo colored. cin overwrites the color information and only writes in the standard gray. It doesn''t look good when my whole screen is printed in a light green and only the user input is light gray until I color it seperately.
Furthermore, I can''t get cin working with *char variable times.
I always have to specify someting like char SomeChar[64]. I prefer char pointers, as they aren''t limited to a certain size.
"Mr Sandman bring me a dream"
I want to be able to make the input echo colored. cin overwrites the color information and only writes in the standard gray. It doesn''t look good when my whole screen is printed in a light green and only the user input is light gray until I color it seperately.
Furthermore, I can''t get cin working with *char variable times.
I always have to specify someting like char SomeChar[64]. I prefer char pointers, as they aren''t limited to a certain size.
"Mr Sandman bring me a dream"
-----------------------------"Mr Sandman bring me a dream"
Pleeeeeaaaaze!!!
Someone MUST have accomplished this before.
If you can''t help me fix my function, can you perhaps suggest a better approach?
This is really urgent!
I need your help!
"Mr Sandman bring me a dream"
Someone MUST have accomplished this before.
If you can''t help me fix my function, can you perhaps suggest a better approach?
This is really urgent!
I need your help!
"Mr Sandman bring me a dream"
-----------------------------"Mr Sandman bring me a dream"
Ok, I got it fixed myself. The fuunction is still a little sticky but it works, basically.
For everyone who might be interested, here's the code:
As the function still doesn't work as accurate as cin, any further comments are appreciated. I'll open a separate thread for this optimisation thing anyhow, but you can answer here aswell.
Edited by - MrSandman666 on October 14, 2000 12:41:09 AM
For everyone who might be interested, here's the code:
char *gengine::GetInput(){ char *Buffer; static char buf[100]; COORD OldCursorPos; COORD StartPos; DWORD BufLength = 0; Buffer = buf; GetConsoleScreenBufferInfo(hOutput, &ScreenBuffer); OldCursorPos.X = ScreenBuffer.dwCursorPosition.X; OldCursorPos.Y = ScreenBuffer.dwCursorPosition.Y; StartPos.X = ScreenBuffer.dwCursorPosition.X; StartPos.Y = ScreenBuffer.dwCursorPosition.Y; ReadConsoleInput(hInput, &record, 1, &cRead); record.EventType = NULL; while (1) { while (record.EventType != KEY_EVENT) ReadConsoleInput(hInput, &record, 1, &cRead); GetConsoleScreenBufferInfo(hOutput, &ScreenBuffer); ReadConsoleInput(hInput, &record, 1, &cRead); if(isalnum(record.Event.KeyEvent.uChar.AsciiChar)) { WriteString(CursorPos.X, CursorPos.Y, &record.Event.KeyEvent.uChar.AsciiChar); CursorPos.X = ScreenBuffer.dwCursorPosition.X + 1; CursorPos.Y = ScreenBuffer.dwCursorPosition.Y; SetConsoleCursorPosition(hOutput, CursorPos); BufLength++; } if (record.Event.KeyEvent.wVirtualKeyCode == VK_BACK) { if (CursorPos.X != OldCursorPos.X) { CursorPos.X = ScreenBuffer.dwCursorPosition.X - 1; CursorPos.Y = ScreenBuffer.dwCursorPosition.Y; SetConsoleCursorPosition(hOutput, CursorPos); BufLength--; } WriteString(CursorPos.X, CursorPos.Y, " "); } else if (record.Event.KeyEvent.wVirtualKeyCode == VK_SPACE) { WriteString(CursorPos.X, CursorPos.Y, " "); CursorPos.X = ScreenBuffer.dwCursorPosition.X + 1; CursorPos.Y = ScreenBuffer.dwCursorPosition.Y; SetConsoleCursorPosition(hOutput, CursorPos); if ((CursorPos.X - 1) == StartPos.X) StartPos.X++; else BufLength++; } else if (record.Event.KeyEvent.wVirtualKeyCode == VK_RETURN) { ReadConsoleOutputCharacter(hOutput, Buffer, BufLength, StartPos, &cRead); break; } record.EventType = NULL; } return(Buffer); }
As the function still doesn't work as accurate as cin, any further comments are appreciated. I'll open a separate thread for this optimisation thing anyhow, but you can answer here aswell.
Edited by - MrSandman666 on October 14, 2000 12:41:09 AM
-----------------------------"Mr Sandman bring me a dream"
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement