Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void WinConsole::process()
- {
- if(winConsoleEnabled)
- {
- DWORD numEvents;
- GetNumberOfConsoleInputEvents(stdIn, &numEvents);
- if(numEvents)
- {
- INPUT_RECORD rec[20];
- char outbuf[512];
- S32 outpos = 0;
- ReadConsoleInput(stdIn, rec, 20, &numEvents);
- DWORD i;
- for(i = 0; i < numEvents; i++)
- {
- if(rec[i].EventType == KEY_EVENT)
- {
- KEY_EVENT_RECORD *ke = &(rec[i].Event.KeyEvent);
- if(ke->bKeyDown)
- {
- switch (ke->uChar.AsciiChar)
- {
- // If no ASCII char, check if it's a handled virtual key
- case 0:
- switch (ke->wVirtualKeyCode)
- {
- // UP ARROW
- case 0x26 :
- // Go to the previous command in the cyclic array
- if ((-- iCmdIndex) < 0)
- iCmdIndex = MAX_CMDS - 1;
- // If this command isn't empty ...
- if (rgCmds[iCmdIndex][0] != '\0')
- {
- // Obliterate current displayed text
- for (S32 i = outpos = 0; i < inpos; i ++)
- {
- outbuf[outpos ++] = '\b';
- outbuf[outpos ++] = ' ';
- outbuf[outpos ++] = '\b';
- }
- // Copy command into command and display buffers
- for (inpos = 0; inpos < (S32)strlen(rgCmds[iCmdIndex]); inpos ++, outpos ++)
- {
- outbuf[outpos] = rgCmds[iCmdIndex][inpos];
- inbuf [inpos ] = rgCmds[iCmdIndex][inpos];
- }
- }
- // If previous is empty, stay on current command
- else if ((++ iCmdIndex) >= MAX_CMDS)
- {
- iCmdIndex = 0;
- }
- break;
- // DOWN ARROW
- case 0x28 : {
- // Go to the next command in the command array, if
- // it isn't empty
- if (rgCmds[iCmdIndex][0] != '\0' && (++ iCmdIndex) >= MAX_CMDS)
- iCmdIndex = 0;
- // Obliterate current displayed text
- for (S32 i = outpos = 0; i < inpos; i ++)
- {
- outbuf[outpos ++] = '\b';
- outbuf[outpos ++] = ' ';
- outbuf[outpos ++] = '\b';
- }
- // Copy command into command and display buffers
- for (inpos = 0; inpos < (S32)strlen(rgCmds[iCmdIndex]); inpos ++, outpos ++)
- {
- outbuf[outpos] = rgCmds[iCmdIndex][inpos];
- inbuf [inpos ] = rgCmds[iCmdIndex][inpos];
- }
- }
- break;
- // LEFT ARROW
- case 0x25 :
- break;
- // RIGHT ARROW
- case 0x27 :
- break;
- default :
- break;
- }
- break;
- case '\b':
- if(inpos > 0)
- {
- outbuf[outpos++] = '\b';
- outbuf[outpos++] = ' ';
- outbuf[outpos++] = '\b';
- inpos--;
- }
- break;
- case '\t':
- // In the output buffer, we're going to have to erase the current line (in case
- // we're cycling through various completions) and write out the whole input
- // buffer, so (inpos * 3) + complen <= 512. Should be OK. The input buffer is
- // also 512 chars long so that constraint will also be fine for the input buffer.
- {
- // Erase the current line.
- U32 i;
- for (i = 0; i < inpos; i++) {
- outbuf[outpos++] = '\b';
- outbuf[outpos++] = ' ';
- outbuf[outpos++] = '\b';
- }
- // Modify the input buffer with the completion.
- U32 maxlen = 512 - (inpos * 3);
- if (ke->dwControlKeyState & SHIFT_PRESSED) {
- inpos = Con::tabComplete(inbuf, inpos, maxlen, false);
- }
- else {
- inpos = Con::tabComplete(inbuf, inpos, maxlen, true);
- }
- // Copy the input buffer to the output.
- for (i = 0; i < inpos; i++) {
- outbuf[outpos++] = inbuf[i];
- }
- }
- break;
- case '\n':
- case '\r':
- outbuf[outpos++] = '\r';
- outbuf[outpos++] = '\n';
- inbuf[inpos] = 0;
- outbuf[outpos] = 0;
- printf("%s", outbuf);
- S32 eventSize;
- eventSize = ConsoleEventHeaderSize;
- dStrcpy(postEvent.data, inbuf);
- postEvent.size = eventSize + dStrlen(inbuf) + 1;
- Game->postEvent(postEvent);
- // If we've gone off the end of our array, wrap
- // back to the beginning
- if (iCmdIndex >= MAX_CMDS)
- iCmdIndex %= MAX_CMDS;
- // Put the new command into the array
- strcpy(rgCmds[iCmdIndex ++], inbuf);
- printf("%s", Con::getVariable("Con::Prompt"));
- inpos = outpos = 0;
- break;
- default:
- inbuf[inpos++] = ke->uChar.AsciiChar;
- outbuf[outpos++] = ke->uChar.AsciiChar;
- break;
- }
- }
- }
- }
- if(outpos)
- {
- outbuf[outpos] = 0;
- printf("%s", outbuf);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment