Archived

This topic is now archived and is closed to further replies.

Getting Keyboard buffer so players can enter a name...

This topic is 5374 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I just want to get what players are typing into the keyboard so they can type in their own name, and file save names etc... but I am not sure how to do this. In the beginners forum i found the following functions to help:
  
RESULT DIInput_FlushKbdBuffer(void)
{  
   HRESULT            hres;
   DWORD              dwItems;  
   char               szScan[7];  
   char               szAge[10];  
   char               szOutput[99];  
   DWORD              k;  
   DIDEVICEOBJECTDATA KbdBuffer[32];
   DIDEVICEOBJECTDATA *lpdidod;  
   dwItems = 32;  // defined as 10

   LPWORD myRes;

   
   Open_Error_File("kbd_bflush.txt");
   		Write_Error("Before GetDevice! %p!\n", KbdBuffer);
		Close_Error_File();  
   hres = lpdikey->GetDeviceData(sizeof(DIDEVICEOBJECTDATA),
                                         KbdBuffer, &dwItems, 0);

   Open_Error_File("kbd_flush.txt");
   		Write_Error("After GetDevice!!!\n");
		Close_Error_File();  
   if(hres !=DI_OK)
   {
	   Open_Error_File("kbd_buffer.txt");
		Write_Error("GetDeviceDataFail!!!\n");
		Close_Error_File();    
   }
   else
	{
	   Open_Error_File("kbd_buffer.txt");
		Write_Error("%p\n", KbdBuffer);
		Close_Error_File();    
   }
   if(hres == DI_BUFFEROVERFLOW)
   {
	   Open_Error_File("kbd_buffer.txt");
		Write_Error("Buffer overflw on flush!!\n");
		Close_Error_File();    
   }

   Open_Error_File("kbd_output.txt");
   for (k = 0; k < dwItems; k++)
   {
      lpdidod = &KbdBuffer[k];    
      sprintf(szOutput, "%d", lpdidod->dwSequence);    
      strcat(szOutput, ". Scan code ");    
      sprintf(szScan, "%x", lpdidod->dwOfs);    
      strcat(szOutput, szScan);        
      if (lpdidod->dwData & 0x80)       
         strcat(szOutput, " pressed ");    
      else      
         strcat(szOutput, " released ");    
      sprintf(szAge, "%d ms ago", GetTickCount() - lpdidod->dwTimeStamp);    
      strcat(szOutput, szAge);     
      //scan2ascii(lpdidod->dwSequence, myRes);

	  Write_Error("%s\n", szOutput);
	
   }  
    Close_Error_File(); 
   return hres;  
}
/*
static int scan2ascii(DWORD scancode, LPWORD result)
{
   static HKL layout=GetKeyboardLayout(0);
   static uchar State[256];

   if (GetKeyboardState(State)==FALSE)
      return 0;
   UINT vk=MapVirtualKeyEx(scancode,1,layout);
   return ToAsciiEx(vk,scancode,State,result,0,layout);
}*/
  
Then in the proper area, I just call : DIInput_FlushKbdBuffer() These two functions are mostly not the ones to be used, the code will be hacked from them once I get these functions working, but to understand the methods Im working on these two functions (just trying to dump what the user types at the title screen out to a errorlog file) The scan2ascii is commented out because the data types are apparently not standard C++ so it doesnt like uchar and stuff... so am I close? not close? what i need to get going on this? thanks.

Share this post


Link to post
Share on other sites
Much simpler, more robust, supports IMEs, supports accessibility options and recommended by MS:

use WM_CHAR Windows messages to handle text input.


I really don''t understand why people write loads of code to try and emulate what is already provided by WM_CHAR when it comes to **text** input.

--
Simon O''Connor
Creative Asylum Ltd
www.creative-asylum.com

Share this post


Link to post
Share on other sites
Okay, so in winproc just intercept the WM_CHAR message and create some sort of buffer that is added to (or subtracted upon backspacE) one character at a time?

I might test this theory in another app before plugging (And fighting with it) in the game, since I haven''t used the WM_CHAR much.

Thanks ill try my best,
Shane

Share this post


Link to post
Share on other sites
Yep. That''s exactly the way. If you have the keyboard acquired as a DInput device you might need to unacquire it.

If you have any doubts as to whether it will work, I''ve personally used it in a number of well known commercial products I worked on which also used DInput for "gameplay" input.

--
Simon O''Connor
Creative Asylum Ltd
www.creative-asylum.com

Share this post


Link to post
Share on other sites
No doubts on it working, just not 100% sure how to implement it yet. Would u have any snippets of code your willing to post? if not, thats cool i understand, just saves me some time from huntin in docs and putzing with my own brass-ackwards implementation of it


-Shane

Share this post


Link to post
Share on other sites
Not much to code really.. just if you get a WM_CHAR message check the wParam. There are a few special things it may send.

0x1B - Escape
0x0A - Linefeed
0x09 - Tab
0x0D - Enter
0x08 - Backspace

Everything else should be a character.

As for keeping a buffer of input, I use an STL string...

string InputBuffer;

So when I get a key.
InputBuffer += wParam;

When I get an 0x08 (backspace) I delete the last character in the string like so.
InputBuffer.erase(InputBuffer.length()-1);

Share this post


Link to post
Share on other sites