Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / Advanced Keyboard Input Handler

Author
Message
zenassem
21
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 19th Apr 2007 08:23 Edited at: 21st Apr 2007 03:26
note: I posted this in the WIP but it got lost amongst the great projects there. It belongs here anyway, and eventually in the codebase.

Version 1.1
Changes from 1.0
1. Added failsafe to keyinfo.txt not existing, program will read default keys via data statements
2. Each key is now set up with a timer.




I hope this code is useful to someone. When coding games with more complexity, it can be difficult (especially for newcomers) to keep track of what is happening at the Keyboard. DBpro native commands alone, don't cover a lot of scenarios by themselves. Including detecting multiple key presses and the like. The Entry$ buffer doesn't serve every purpose either.

So I decided to spend some time and code a Keyboard Input Handler. Basically it's group of functions (like a library), that allow coders a more stream-lined, simplified, and accurate way to read input from the keyboard and perform logic based on the results.


Please feel free to give comments and suggestions.

It's almost complete. Things I am currently working on
-The individual key timers

-A second array that lets users decide which keys to enable from a file. That will also allow them to customize the key names to jump, fire, or whatever. This will increase efficiency, as right now the code goes from0 to 255 to process scancodes. A lot of which don't even exist.

-Finding Combos within the string (for fighting games and such). Right now it looks for an exact match. It doesn't find codes within a longer string.

-Some other tweaks, and code cleanup.

The code contains a main loop that tests out some of the functions.
Attached is a .txt file. This file, will eventually be used by the programmer, to define which keys need to be monitored as well as custom names.

Attachments

Login to view attachments
Essence
22
Years of Service
User Offline
Joined: 12th Oct 2002
Location: The Netherlands
Posted: 19th Apr 2007 21:35
wow

thats some nice work there :o
Mind if I implent this in the MiREVU Framework? :y

Current project: MiRevu Visual Gaming Engine
thread: http://forum.thegamecreators.com/?m=forum_view&t=103073&b=8
zenassem
21
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 19th Apr 2007 22:21
Thanks.

Yes, you can use it. Do you have to link up MiREVU?

I have a couple of things to implement and optimize. Should take me a couple of days.
Essence
22
Years of Service
User Offline
Joined: 12th Oct 2002
Location: The Netherlands
Posted: 20th Apr 2007 18:12
Ehm... Well, I made a game about it, but I'm working on a 'framework' that everyone can use in their games, or actually to build their games on.

This includes a set of new commands, a console (quake 3 engine style) and some other usefull stuff.

I have a webhost, but I didnt upload the project yet.
You can add me on msn if you want or either contact me by email.
msn: evaflux@gmail.com
email: h.iedema@gmail.com

Current project: MiRevu Visual Gaming Engine
thread: http://forum.thegamecreators.com/?m=forum_view&t=103073&b=8
zenassem
21
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 20th Apr 2007 18:51
Great,

I will contact you. I'd be interested in learning more about your ideas, and framwork. It's kind of what I've been thinking of, and the reason I started the KBIH. Perhaps we can collaborate. I'll be finsihing up featueres & tweaks of the KBIH in the next day or so. If you have any suggestions, for more features and/or functions let me know.

Also let me know what you think of my coding conventions. I tend to use:

p.s (I can also code shortcut versions of the functions)
eg.
f_IsKeyPressed() to f_IKP() or simply IKP()


------
THIS_IS_CONSTANT : for constants

this_IsType : for UDT's

f_CallFunction() for functions (though I might add I B S F etc..) for return type. For example a function that returns Integer Bool, String$, Float)
fI_CallFunction() : return type Integer
fB_CallFunction() : return type Booolean
fS_CallFunction() : return type string

Important Variables:
MyVariable

Unimportant Variables:
myvariable
index
or
ival :for Integer value
strval : for String value

Ar_MyArray : for arrays

retval : for integer return from function
retbool : for boolean return from function
retstr : for string return from function
-----

(!) remove the "X"'s : used to confuse forum scanning email-bots

Hotmail: zenassemXX@hotmail.com
Gmail: ejomX.dioxinX@gmail.com
MSN: I have a couple of alliases so I'll have to double check.
Essence
22
Years of Service
User Offline
Joined: 12th Oct 2002
Location: The Netherlands
Posted: 20th Apr 2007 19:13
Hmm
A suggestion to your code.
I just tried it out, but It seems like the text file (which defines the keys) HAS to be present. If not, well..you know.

How about making a boolean variable which determines wheter to load or not to load that txt file, and if not, use some default settings.

Apart from that, it works great.

Current project: MiRevu Visual Gaming Engine
thread: http://forum.thegamecreators.com/?m=forum_view&t=103073&b=8
zenassem
21
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 20th Apr 2007 19:31 Edited at: 20th Apr 2007 19:33
Yes I can do that. I did provide the text file in the firsr post as a download attachment. Here it is in a code snippet if you want it to try it out.

just save it in notepad as keyinfo.txt


That file will be more important at completion, where the user will use it to define which keys, they want to monitor, and the name that they would like to refer to the key eg. Fire, Up, Jump etc...

I can easily remove it from the code, and fill the array in-code, but since it will be important later, i decided to leave it.
Essence
22
Years of Service
User Offline
Joined: 12th Oct 2002
Location: The Netherlands
Posted: 20th Apr 2007 23:06
lol
I knew that =p

What I meant, is wether to use the file at all or not.
Cuz, when you have a key pressed now thats not in the array, you'll get a dbpro error about the array exceeding its limit etc.

Current project: MiRevu Visual Gaming Engine
thread: http://forum.thegamecreators.com/?m=forum_view&t=103073&b=8
zenassem
21
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 20th Apr 2007 23:50
Thx I'll fix that error. Do you know what key or more importantly the scancode of the key you pressed that gave you the error? Cause as of right now, the array is built from 0-255 or (256) elements and filled with nulls. Then the program reads the file and overwrites the values. So unless there are scancodes higher than 255 you shouldn't get an error. I'll recheck the code as well.

Also the code is not complete just yet. Amongst the other things I mentioned I'm working on, I'll add a check for the file exist, if not it will load a default array from data statements or something.
Eventually the code will not look at all 256 locations, but will only look at keys that the user enables in the keyinfo.txt file (if it exists. lol) or it will default to the data statements as a fail-safe.
zenassem
21
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 21st Apr 2007 01:42 Edited at: 21st Apr 2007 03:28
Edit: fixed mistakes and other changes have been posted in first post. Version 1.1

Thanks Essence, you made the Thank You list (see comments in code at the top)!!!!

Ok. I found why you could get a scancode error.

The bolded line should have been:
kb_KeyInfo(index).ScnCode=index

dim kb_KeyInfo(MAX_NUM_KEYS) as KeyType

for index=0 to MAX_NUM_KEYS
kb_KeyInfo(index).ScnCode=0
kb_KeyInfo(index).KeyName="NULL"
kb_KeyInfo(index).IsPressed=FALSE
kb_KeyInfo(index).IsReleased=NSET
kb_KeyInfo(index).IsHeld=FALSE
kB_KeyInfo(index).KeyTimeInit=0
kb_KeyInfo(index).KeyTimeHeld=0
next index

That will ensure all scancodes 0 to 255 will get created with null values.

Login to post a reply

Server time is: 2024-11-22 18:55:17
Your offset time is: 2024-11-22 18:55:17