PDA

View Full Version : Recording what user types, for chat



Diaboli
23-07-2007, 08:03 PM
System: Windows Vista, Intel Centrino Duo 2.16, Nvidia GeForce 7950 512mb, 2048mb ram
Compiler/IDE: BDS 2006
Using: UnDelphiX

Problem: How to get string input for chat?

I have a timer that updates the inputstates every 33 ms, wich works for movement and short commands, but how can i make a input for a string??

Diaboli...

cronodragon
23-07-2007, 09:45 PM
Hello!

Just store each ascii representation of each key in a string to start. Something like:

line := line+asciiKey;

Diaboli
23-07-2007, 10:17 PM
well, that easy to imagine (as it is how i thought of it), but i dont actually HAVE the ascii representation of any keys...

the program check keystates every 33 milliseconds, not with a onkeydown event... (as i have been using for this kind of thing in the past...)

cronodragon
23-07-2007, 10:31 PM
How does it check the states, does it scans all the keys from 0 to 255? If so, just make an array that matches every key number to the corresponding ascii code and get the ascii value from it.

Diaboli
23-07-2007, 10:41 PM
there is this:
Input.Keyboard.Keys: Array of Boolean

but i dont know what integers are wich letters...

cronodragon
23-07-2007, 11:09 PM
Take a look at: http://msdn2.microsoft.com/en-us/library/bb174685.aspx

There is the whole list in order from 0 to 255. Just make you array in such way that it matches those ascii codes, like this:

const
AsciiKey : array[0..255] of Char =
'0', // DIK_0,
'1', // DIK_1,
'2', // DIK_2,
'3', // DIK_3,
'4', // DIK_4,
'5', // DIK_5,
'6', // DIK_6,
'7', // DIK_7,
'8', // DIK_8,
'9', // DIK_9,
'A', // DIK_A,
#0, // DIK_ABNT_C1,
#0, // DIK_ABNT_C2,
... etc

Those with value of #0 don't have ascii code. To convert your codes to Ascii just index the array with the code of which key you are getting a press.

tpascal
23-07-2007, 11:19 PM
however just cheking the keystate in a timer seem is not a readeable way to get the string the user is typing; most likely you will get a string like "tthisss iss aaa tteesst sstriinngg"; you have to take note when the key is realeased before recording again that key...still there is the problem about replicate key rate.

Diaboli
23-07-2007, 11:36 PM
tpascal: u have a very good point... i would much rather have some kind of keyup or keypress event, but how can i make that?

like: how do you get the players name in your highscore-lists and such? there you must have the kind of code i need...

cronodragon
24-07-2007, 03:38 PM
Making one of those grids to enter the highscore is a little easier to build, but you still need to solve the same problems you will find making an input field.

User137
25-07-2007, 02:36 PM
Take a look at: http://msdn2.microsoft.com/en-us/library/bb174685.aspx

There is the whole list in order from 0 to 255. Just make you array in such way that it matches those ascii codes, like this:

const
AsciiKey : array[0..255] of Char =
'0', // DIK_0,
'1', // DIK_1,
'2', // DIK_2,
'3', // DIK_3,
'4', // DIK_4,
'5', // DIK_5,
'6', // DIK_6,
'7', // DIK_7,
'8', // DIK_8,
'9', // DIK_9,
'A', // DIK_A,
#0, // DIK_ABNT_C1,
#0, // DIK_ABNT_C2,
... etc

Those with value of #0 don't have ascii code. To convert your codes to Ascii just index the array with the code of which key you are getting a press.

That is unnecessary since chr(byte) already tells what each ascii number means as a letter.

You said Input.Keyboard.Keys so i assume keys are read with (un)DelphiX. Anyway this is 1 way of making key down and up events:
// Used variables:
var
keyDown: array[0..255] of boolean;
i: integer;
chatLine: string;

...
with Input.Keyboard do
for i:=0 to 255 do
if keys[i] and (not keyDown[i]) then begin
keyDown[i]:=true; // KeyDown here

// Simple input processing (0..31 are not really characters)
if i in [32..255] then
chatLine:=chatLine+chr(i);

end else if (not keys[i]) and keyDown[i] then begin
keyDown[i]:=false; // KeyUp here

end;

cronodragon
25-07-2007, 03:09 PM
Take a look at: http://msdn2.microsoft.com/en-us/library/bb174685.aspx

There is the whole list in order from 0 to 255. Just make you array in such way that it matches those ascii codes, like this:

const
AsciiKey : array[0..255] of Char =
'0', // DIK_0,
'1', // DIK_1,
'2', // DIK_2,
'3', // DIK_3,
'4', // DIK_4,
'5', // DIK_5,
'6', // DIK_6,
'7', // DIK_7,
'8', // DIK_8,
'9', // DIK_9,
'A', // DIK_A,
#0, // DIK_ABNT_C1,
#0, // DIK_ABNT_C2,
... etc

Those with value of #0 don't have ascii code. To convert your codes to Ascii just index the array with the code of which key you are getting a press.

That is unnecessary since chr(byte) already tells what each ascii number means as a letter.

But using those DIK_ values, the code for A would be 10, not 65 as it would be required by Chr() which is just making a typecast, not a translation.

User137
25-07-2007, 05:25 PM
Typecasting would still be unneeded :wink: Since it's already been made as variable via Virtual Keycodes
http://delphi.about.com/od/objectpascalide/l/blvkc.htm

ie: VK_RETURN, VK_BACK, VK_ESCAPE... ord('a') would numerize key 'a'.

Mirage
26-07-2007, 08:54 AM
Handle WM_CHAR message to get characters which user types taking in account current keyboard layout, caps lock status etc.