Custom Keymapping

Blitz3D Forums/Blitz3D Programming/Custom Keymapping

Chroma(Posted 2009) [#1]
I was perusing the archives and was looking at some project plasma code and saw a line that said keymap[10]. A light went on and it inspired me to write this. It's kinda like the guy building that mountain of mashed potatoes in Close Encounters of the Third Kind (just a warning). It'd be easy to read a keymap.ini into this so you can have saveable custom keymapping in your game. One question though is how would you handle joystick buttons without created a separate joybutton checker...if that's possible.

So what are you opinions?

EDIT: See new code below.


Chroma(Posted 2009) [#2]
Here's a much improved version that allows custom key, mouse, and joystick button inputs that can be loaded from an .ini.

Pretty sure this can't be optimized further.

EDIT: See below.


Chroma(Posted 2009) [#3]
Ok I'm home now and here's the actual working code. Works great.

; Custom Input Mapping

Const KEY_INPUT%   = 1
Const MOUSE_INPUT% = 2
Const JOY_INPUT%   = 3

Dim InputMap(32,2,2)


Const ACTION_GEARDOWN% = 1

Const KEY_G% = 34


Graphics3D 640,480
SetBuffer BackBuffer()

SetInputMapPrimary(ACTION_GEARDOWN, KEY_INPUT, KEY_G)

While Not KeyHit(1)


If InputMapHit(ACTION_GEARDOWN) Then End


RenderWorld
Flip 0
Wend
End


Function SetInputMapPrimary(ActionID%, InputType%, EventID%)
	InputMap(ActionID,0,0) = InputType
	InputMap(ActionID,0,1) = EventID
End Function

Function SetInputMapAlternate(ActionID%, InputType%, EventID%)
	InputMap(ActionID,1,0) = InputType
	InputMap(ActionID,1,1) = EventID
End Function

Function InputMapHit%(ActionID%)
	
	;Check Primary Input
	If InputMap(ActionID,0,0)
		Select InputMap(ActionID,0,0)
			Case KEY_INPUT Return KeyHit(InputMap(ActionID,0,1))
			Case MOUSE_INPUT Return MouseHit(InputMap(ActionID,0,1))
			Case JOY_INPUT Return JoyHit(InputMap(ActionID,0,1))
		End Select	
	EndIf
	
	;Check Alternate Input
	If InputMap(ActionID,1,0)
		Select InputMap(ActionID,1,0)
			Case KEY_INPUT Return KeyHit(InputMap(ActionID,1,1))
			Case MOUSE_INPUT Return MouseHit(InputMap(ActionID,1,1))
			Case JOY_INPUT Return JoyHit(InputMap(ActionID,1,1))
		End Select
	EndIf
	Return False
End Function



Chroma(Posted 2009) [#4]
In my quest to write a custom key mapper I also wrote this which returns the number of the scancode you just pressed. I find it very handy for when you want to work in scancodes only and need to know which key, mouse, or joy code the user just pressed so you can store it.

Function GetScanCode%()
	Local sc%
	For sc = 1 To 212
		If KeyDown(sc) Then Return sc
	Next
End Function

Function GetMouseCode%()
	Local mc%
	For mc = 1 To 3
		If MouseDown(mc) Then Return mc
	Next
End Function

Function GetJoyCode%()
	Local jc%
	For jc = 1 To 16
		If JoyDown(jc) Then Return jc
	Next
End Function




Warner(Posted 2009) [#5]
Looks good. Maybe it is something to post in the code archives?


Chroma(Posted 2009) [#6]
Yup, I'm hoping to get to that point soon. I've been meaning to tackle this for a couple of years now.