Making a textbox work correctly

Started by
2 comments, last by Burnt_Fyr 11 years, 12 months ago
I wasn't sure which forum to post this under

Hey, i made textbox class. It works the way i want it to, but there's 2 problems. I can't figure out how to stop the keyboard input to repeat the characters extremely fast, just pushing one letter as quick as a possibly can gives me like 20 of those characters in the textbox.
I know that 'GetAsyncKeyState()' is supposed to rely on the windows character repeat rate and things like that. But when i use it here it's not working like that at all.

// Problem 1


// This is exactly what happens in the textbox, no key pressed returns 0
void Textbox::Update()
{
// Get input from the keyboard
char ch = input->KeyPress();

if(ch)
{
if(ch == Backspace)
text.pop_back();
else
text += ch;
}
}

/**** I want to push a button, it prints that character once, keep holding that button and after a second or so it starts repeating at a certain rate ****/



// Problem 2


// I spent 7 hours trying to figure this one out and 2 hours searching the web for it =\
// Calculating the position of the cursor, i know it relies on the size of the text, but what about the font being used, they're all different sizes as
//well

// This is what I figure is the basic form of the equation
cursor.x = strlen(text)*fontSize ;



Ive been on this thing for a couple days now, any help would be awesome.
Advertisement
You don't show your code that actually deals with GetAsyncKeyState, but the documentation says the MSB indicates whether the key is pressed, whilst the LSB indicates whether it was pressed since the last call. Presumably you're not checking the LSB? The answer to your second question is surely going to rely on how you're doing your text rendering...
[TheUnbeliever]
To avoid complications temporarily, i simply do this with keyboard input



// This gives me a character like every 5seconds when i hold the key down
if(GetAsyncKeyboardState('A') & 1) return 'a'; // Repeat for every character, also tried '& 0x80'

// This works perfectly
if( (GetAsyncKeyState('A') & 1) && ( GetAsyncKeyState(VK_LSHIFT) || GetAsyncKeyState(VK_RSHIFT) ) ) return 'A';


// My text rendering is EXACTLY like this
ID3DXFont *font;
IDirect3DDevice9 *d3dDevice;
ID3DXSprite *spriteManager;
ID3DXCreateFontA(d3dDevice, 15, 0, FW_NORMAL, NULL, false,DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,"Times New Roman", &font);

string text;
D3DCOLOR fontColor(1,1,1,1);
font->DrawTextA(spriteManager, text.c_str(),-1,NULL,DT_LEFT,fontColor);


I'm not sure what LSB and MSB are, but i'm guessing im not checking the LSB? lol

To avoid complications temporarily, i simply do this with keyboard input



// This gives me a character like every 5seconds when i hold the key down
if(GetAsyncKeyboardState('A') & 1) return 'a'; // Repeat for every character, also tried '& 0x80'

// This works perfectly
if( (GetAsyncKeyState('A') & 1) && ( GetAsyncKeyState(VK_LSHIFT) || GetAsyncKeyState(VK_RSHIFT) ) ) return 'A';


// My text rendering is EXACTLY like this
ID3DXFont *font;
IDirect3DDevice9 *d3dDevice;
ID3DXSprite *spriteManager;
ID3DXCreateFontA(d3dDevice, 15, 0, FW_NORMAL, NULL, false,DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,"Times New Roman", &font);

string text;
D3DCOLOR fontColor(1,1,1,1);
font->DrawTextA(spriteManager, text.c_str(),-1,NULL,DT_LEFT,fontColor);


I'm not sure what LSB and MSB are, but i'm guessing im not checking the LSB? lol


Least significant bit, and most significant bit, respectively.


Although the least significant bit of the return value indicates whether the key has been pressed since the last query, due to the pre-emptive multitasking nature of Windows, another application can call GetAsyncKeyState and receive the "recently pressed" bit instead of your application. The behavior of the least significant bit of the return value is retained strictly for compatibility with 16-bit Windows applications (which are non-preemptive) and should not be relied upon.
[/quote]

This topic is closed to new replies.

Advertisement