Input (Getkey ?)

BlitzMax Forums/BlitzMax Beginners Area/Input (Getkey ?)

AdrianT(Posted 2004) [#1]
Thought I'd start programming now that Bmax is out (been 20 years lol). Anyway starting from the beginning I've been doing the usual starter tutorials only printing them to the screen with drawtext rather than just checking the output console.

At the moment I'm stuck on input and having searched on the BB and BC forums, it seems the best way to draw input on the screen in previous blitz languages was to use the GetKey command.

Doesn't seem to be an equivalent to Getkey in Bmax so I thought I'd ask what the Bmax solution would be.


Kanati(Posted 2004) [#2]
Nobody seems to have an answer to this one... I've been peeking around in the bmx and c files and it does appear to have just vanished...


Kanati(Posted 2004) [#3]
I have a working System mod with GetKey now... I think. :)

Still tweaking the C and BMX stuffs... But I don't think I'm gonna hand it out. This is something Mark could add in a few minutes and my hacking about in his module probably isn't a good idea. :)

Eh... It works, but it doesn't handle multiple keys down at once or control-keys right now. Mark really needs to add this one himself but mine will hold me over til he does.

Kanati


AdrianT(Posted 2004) [#4]
heh, maybe it should go in the bug section if Mark is more likely to look at stuff there. I just don't like posting stuff in the bug section when I havent got a clue what I'm doing most of the time lol.


Diordna(Posted 2004) [#5]
Post your routine please!


flying willy(Posted 2004) [#6]
Could it be that BRL actually forgot this one?


skidracer(Posted 2004) [#7]
The bpaint sample has a keyboard input routine but yes, we need a better solution.


Kanati(Posted 2004) [#8]
Mine is really a kludge I added into the system.win32.c, system.win32.bmx, system.bmx and ummm... I think something had to go into driver.bmx too. It's sitting on my laptop at work and if there's something that will work in the bpaint thingy (I'll look tomorrow) that might work better... You'd be better off using that.

I *will* post the changes I made tomorrow if you want them, but I really really really don't think futzing about in the system mod is a great idea. As soon as Mark makes changes to it and you synch up... it'll be broke.


AdrianT(Posted 2004) [#9]
Yeah I think I'll wait, I can't imagine that it will be that long.


bradford6(Posted 2004) [#10]
try this:
not perfect but does the job


Graphics 640,480,0

foo$ = gl_input$(10,10,"what is your name? ")

SetColor 255,255,0 ; SetScale(2,2) 

DrawText "hello "+foo$+", how are you?",100,100

flip
waitmouse

End 


'-------------------------------------
Function gl_input$(x,y,prompt$ = "?")

	Repeat
	
		Cls
		DrawText prompt$+m$,10,10
		DrawText key,10,50
		hit_key = 0
		For key = 1 To 226
		
			hit_key = KeyHit(key)
			If hit_key 
				m$ = m$ + Chr(key)
			
				If key = KEY_ENTER
					Return m$
				EndIf
				If key = KEY_BACKSPACE
					l = Len(m$)
					m = m[..l-2]
				EndIf
			EndIf 
		Next
	
 	Flip
	Until KeyHit(KEY_ESCAPE)

End Function



AntonyWells(Posted 2004) [#11]
Here's a public module, i knocked up earlier.
You can drop into a folder called input.mod in the mod\pub folder.
Call update once per frame, then getkey to get the key returned. should be faster than doing it every getkey in big apps with lots of seperate checks.

Module pub.input

ModuleInfo "Version: 1.00"
ModuleInfo "Author:Antony Wells"
ModuleInfo "License:Public Domain"
ModuleInfo "Copyright:Poblic Domain" 
ModuleInfo "Modserver: BRL/Anyone"

Type GetKey
   Method Update:()
     lastKey=0
     For j=1 To 255
         KeyMap[j]=KeyHit(j)
         If KeyMap[j] LastKey=J
     Next
     FlushKeys 'Needed?
   End Method
   Method GetKey:Int()
       Return LastKey
   End Method
   'Returns how many times the key was hit since last update. 
   Method KeyHit:Int(Key:Int)
       Return KeyMap[Key]
   End Method
   Local LastKey
   Local KeyMap:Int[255]
End Type



Kanati(Posted 2004) [#12]
Those are certainly workable for the short term... But you really need to do what I did without breaking into Mark's system module... By that I mean you need to hook windows and intercept the messages as Mark does by grabbing WM_KEYDOWN and WM_KEYUP messages. You definitely don't want to be looping 255 times through the KeyHit() routine. I can't imagine that being very fast in comparison to simply checking the windows messages and reporting it back once.


Kanati(Posted 2004) [#13]
What I did to the C code is super simple... I just added

static unsigned int getkeycode[2];

unsigned int  *bbSystemGetKey=getkeycode;


to the declares...

In the select for WM_KEYDOWN and WM_SYSKEYDOWN I added

getkeycode[1] = wp;


In the select for WM_KEYUP and WM_SYSKEYUP I added
getkeycode[1] = 0;


That's it for System.Win32.c

Just like in Blitz3D and Blitz+, it doesn't handle shift, ctrl, etc, though it does return their keycodes when they are hit by themselves. It just doesn't handle multiple combinations. Anyway... The other changes...

In driver.bmx in the TSystemDriver I added...

Method GetKey( ) Abstract


In System.bmx I added...

Rem
bbdoc: Check for general key down state
returns: keycode of key pressed
End Rem
Function GetKey( )
  If auto_poll PollSystem
  Return Driver.GetKey( )
End Function


In System.Win32.bmx I added the following to the Extern block...

Global bbSystemGetKey:Int Ptr="bbSystemGetKey"


And I added this as well to System.win32.bmx

Method GetKey()
  Return bbSystemGetKey[1]
End Method



Craptacular code I know (why I used a 2 position array I don't know but I didn't feel like going back and redoing it.) but it does work... If someone wants to take a look at that handleMsg() routine in System.Win32.c and break it out or whatever into their own public module, THAT would be the way to do it. I haven't had time to look into it myself yet but if we don't see one from BR soon I'll have a go at it. Just been quite some time since I've done much with C. :)

I was thinking... return an int containing the keycode in the rightmost 8 bits with the leftmost bits containing flags for the special keys like shift, ctrl, alt, etc.

Of course I haven't even GLANCED at the mac code yet, but I would have to believe it would be SOMEWHAT similar.

Kanati


Warren(Posted 2005) [#14]
Has anyone been able to come up with something that is cross platform and works the same as GetKey() used to? It's pretty irritating that we can't do something as simple as keyboard input...


bruZard(Posted 2005) [#15]
oh my god, now very ugly english:

i've get the code from the sample by simon armstrong ... but: it only let me use english keybord settings (where's my Ä, Ö, Ü, ß?) ... i dont want to use the winapi to do that ... where is the solution of that? help me ... please! :)


Panno(Posted 2005) [#16]
ah bruz ...

no ä,ü,ß support by Bmax this time see this

drawtext "BMÄX",100,100

is BMX @ 100,100


Kanati(Posted 2005) [#17]
WarrenM

I don't know if this works or not... But someone in the thread said it did. If so, consider it a kanati-special as it was my first attempt and writing a mod. And I *think* that it should probably be cross platform compatible.

http://www.blitzbasic.com/Community/posts.php?topic=42128


Warren(Posted 2005) [#18]
Interesting, I must have missed that! Thanks, I'll check that out over the weekend...


ghislain(Posted 2005) [#19]
Another variant...

WaitKeyArray ( [key_left, key_right, key_up, key_down] )
 ' returns true if one of the keys in the array pressed

function KeyHitByArray ( a[] )
 for local b = eachin a
  if keyhit (b) = true then return b
 next
end function

function WaitKeyByArray ( a[] )
 local i = waitkey()
 for local b = eachin a
  if i = b then return b
 next
end function


I don't know if it is of any use...
Ghislain


Kanati(Posted 2005) [#20]
anyone try the mod I posted? I don't feel like messing with it but if it doesn't work I won't keep pointing to it when someone asks about getkey. :)


fredborg(Posted 2005) [#21]
Kanati: It doesn't work for me :)

When using graphics mode, there is no return value ie. it's 0 all the time.

When using a real console window it returns 1 on every key.


TeaVirus(Posted 2005) [#22]
I get the same results that fredborg does.


Kanati(Posted 2005) [#23]
well then... that solves that. :)


Warren(Posted 2005) [#24]
Grrr...


Dirk Krause(Posted 2005) [#25]
Kanatis code does work - at least in the console. I didn't try in Graphics mode. Unfortunately I don't have the code anymore, but I'll check right away.


Dirk Krause(Posted 2005) [#26]
ok, here goes.

I created a directories dirk.mod\test.mod in the mod main directory. Then I put the getkey.c into it with
#include <conio.h>

int bbGetKey(void) {
  return kbhit();
}

Then another file test.bmx with
Module dirk.test

Import "getkey.c"

Extern
  Function bbGetKey:Int()
End Extern

Function GetKey:Int()
  Return bbGetKey()
End Function

Then I ran my test code
Repeat
  a = bbGetKey:Int()
  If a <> 0 Then
    Print a
  EndIf
Forever
which worked fine on the console.
[EDIT]I was refering to Kanati's code in http://www.blitzbasic.com/Community/posts.php?topic=42128, the other thread. I didn't try the code above.


Kanati(Posted 2005) [#27]
I'm looking at hooking windows messages like Mark did in his system32 mod... But it definitely won't be cross platform. :(


Warren(Posted 2005) [#28]
Is BRL even reading these GetKey threads? I mean, it really is pretty irritating to not be able to accept simple keyboard input from the player.


tonyg(Posted 2005) [#29]
Hopefully they read the bug reports...
http://www.blitzbasic.com/Community/posts.php?topic=41822
I know an updated beta is due soon so it might be a case of wait and see before bringing it to BRL attention again.