Sign in to follow this  

Making a textbox work correctly

This topic is 2054 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 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
[code]

// 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 ****/

[/code]

// Problem 2
[code]

// 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 ;

[/code]

Ive been on this thing for a couple days now, any help would be awesome.

Share this post


Link to post
Share on other sites
You don't show your code that actually deals with GetAsyncKeyState, but [url="http://msdn.microsoft.com/en-us/library/windows/desktop/ms646293%28v=vs.85%29.aspx"]the documentation[/url] 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...

Share this post


Link to post
Share on other sites
To avoid complications temporarily, i simply do this with keyboard input

[code]

// 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);
[/code]

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

Share this post


Link to post
Share on other sites
[quote name='Muzzy A' timestamp='1335743182' post='4935934']
To avoid complications temporarily, i simply do this with keyboard input

[code]

// 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);
[/code]

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

Least significant bit, and most significant bit, respectively.

[quote]
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 [b]GetAsyncKeyState[/b] 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]

Share this post


Link to post
Share on other sites

This topic is 2054 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this