user defined keys

BlitzMax Forums/BlitzMax Beginners Area/user defined keys

I'm adding to my game the ability for users to define their own keys, but have hit a bit of a brick wall. I can't work out how to do it at all. This is what i've done so far in a little test program.

(code)
Graphics 640,480,0,60
Local upkey%

While Not KeyHit(key_escape)

upkey=GetChar()

If KeyDown(upkey) Then DrawText "up",10,10

Flip
Cls
Wend
(/code)

The above doesn't work, and im a bit stumped as to what to try next

any suggestions/code examples?

cheers
Charlie

You could use upkey=waitkey() which will wait until the user hits a key and return the integer value.
Getchar returns the ascii code and doesn't sem to be mapped for the cursor keys.

thanks very much, i hadnt thought of looking for waitkey

cheers
charlie

Edit: Wait, think I got the wrong end of the stick. Sorry.

if you have a look at the help for keydown and keyhit, there are specific key codes for each key. the up key is KEY_UP, so
 If KeyDown(KEY_UP) Then DrawText "up",10,10

will work. To assign user specified keys you can use an array like:
local playerOneKeys[]=[KEY_UP,KEY_DOWN,KEY_LEFT,KEY_RIGHT,KEY_ENTER]

if keydown(playerOneKeys[0])
debuglog "player has pressed their up key."
endif

Then you can change it easily by going:
playerOneKeys[4]=KEY_F

which will change the player's first key from ENTER to F

That what you were thinking of?

yes, but its just a different way of doing the same thing.

thanks a lot
charlie

Rem
bbdoc: THashEntry Type
about: The THashEntry Type is used in a THash and stores the keynames and values <br> Fields: Key:String  and Value:String
EndRem
Type THashEntry
	Field Name : String
	Field Value : Int
End Type

Rem
bbdoc: THash Type
about: The THash Type is an extension of TList and so can be treated as such (with Eachin, etc).
EndRem
Type THash Extends TList
	Rem
	bbdoc: Method Add
	about: returns a TLink pointing to a THashEntry<br> pass the Key and Value with optional AutoReplace (True/False)<br>
			NOTE:Autoreplace has not yet been implemented
	EndRem
	Method Add:TLink(Name:String, Value:Int, AutoReplace=False)
		'check to see if key already exists, 
		'if it exists and AutoReplace=True
		'then replace it, else exit the method 
		'returning the existing link		
		For Local he:Object = EachIn Self
			If THashEntry(he).Name = Name Then 
				THashEntry(he).Value = Value
				Return FindLink(he)
			EndIf
		Next
		
		Local tempEntry:THashEntry = New THashEntry
			tempEntry.Name = Name
			tempEntry.Value = Value
		
		Return InsertBeforeLink(tempEntry,_head )
	End Method

	Rem
	bbdoc: Method Get
	about: Returns the Value of the supplied Keyname (String)
	EndRem
	Method Get:Int(Name : String)
		Local result : Int = 0
		Local he : THashEntry
			For he = EachIn Self
				If he.Name = Name Then
					result = he.Value
					Exit
				EndIf
				FlushMem
			Next
		Return result
	End Method
	
End Type



Type TKeys
	Field Keys:THash
	
	
	Method SetKey(KeyName:String, KeyValue:Int)
		Keys.Add(KeyName, KeyValue)
	End Method
	
	
	Method GetKey:Int(KeyName:String)
		Return Keys.Get(KeyName)
	End Method
	
	
	Method GetKeyDown:Int()
		Local Count:Int
		
		While Count < 192
			If KeyDown(count) Then Exit
			Count:+1
		Wend
		
		Return Count
	End Method
	
	
	Function Create:TKeys()
		Local tempKeys:TKeys = New TKeys
			tempKeys.Keys = New THash
		Return tempKeys
	End Function
End Type


Local myKeys:TKeys = TKeys.Create()
	myKeys.SetKey("Up", KEY_UP)
	myKeys.SetKey("Down", KEY_DOWN)
	myKeys.SetKey("Left", KEY_LEFT)
	myKeys.SetKey("Right", KEY_RIGHT)
	myKeys.SetKey("Fire", KEY_SPACE)
	myKeys.SetKey("Quit", KEY_ESCAPE)


Type TPlayer
	Field X:Float = 100
	Field Y:Float = 100
End Type

Global player1:TPlayer = New TPlayer

Graphics 640,480,0,0

While Not KeyDown(myKeys.GetKey("Quit"))
	Cls
	
	DrawOval(player1.X, player1.Y, 50,50)
	
	If KeyDown(myKeys.GetKey("Up")) Then player1.Y :- 5.0
	If KeyDown(myKeys.GetKey("Down")) Then player1.Y :+ 5.0
	If KeyDown(myKeys.GetKey("Left")) Then player1.X :- 5.0
	If KeyDown(myKeys.GetKey("Right")) Then player1.X :+ 5.0
	
	Flip
Wend

EndGraphics

End