PDA

View Full Version : console_inkey & full keyboard support



oli
27-11-2009, 17:05
Hi folks,

I have problems with console_inkey. It works fine with most of the keyboard, but I don't get
anything returned for some keys, like "." or "+".
Is this a known issue?

Kind regards,
Oli

ErosOlmi
27-11-2009, 17:38
Ciao oli and welcome to thinBasic community.

Console_Inkey (http://www.thinbasic.com/public/products/thinBasic/help/html/console_inkey.htm), in case of extended keyboard pressing, returns a 2 bytes string where first byte is null while second byte is the virtual-key code.
You can get values of virtual-key codes from http://msdn.microsoft.com/en-us/library/ms645540(VS.85).aspx

Unfortunately I've setup virtual-key codes in UI module and not in Console module. And also I've forgot to add OEM virtual-key codes (I will add in next update).
I will consider to move those virtual-key codes in thinBasic Core engine so they will be available to all modules.

Anyway, "." and "+" and other codes are :

%VK_OEM_PLUS = &HBB ' "+" any country
%VK_OEM_COMMA = &HBC ' "," any country
%VK_OEM_MINUS = &HBD ' "-" any country
%VK_OEM_PERIOD = &HBE ' "." any country

So your code can be something like:


Uses "Console"

dim s as string
dim i as long

%VK_OEM_1 = &HBA ' ";:" for US
%VK_OEM_PLUS = &HBB ' "+" any country
%VK_OEM_COMMA = &HBC ' "," any country
%VK_OEM_MINUS = &HBD ' "-" any country
%VK_OEM_PERIOD = &HBE ' "." any country


PrintL "Please press 'q' to exit."

DO
s = Console_InKey()
sleep(0)
i = len(s)
select case i
case 1
PrintL "You pressed the " + s + " key."
case 2
Print "You pressed an extended key: " + Hex$(Asc(s, 2))
'--Check second char of returned string
Select Case Asc(s, 2)
Case %VK_OEM_PLUS
PrintL " + (plus)"
Case %VK_OEM_COMMA
PrintL " , (comma)"
Case %VK_OEM_MINUS
PrintL " - (minus)"
Case %VK_OEM_PERIOD
PrintL " . (period)"
Case Else
PrintL " ... something else"
End Select
case 3
select case asc(right$(s, 1))
case %CONSOLE_MOUSE_MOVED
PrintL "You moved the mouse to" + Str$(Asc(LEFT$(s,1))) + "," + LTrim$(Str$(Asc(Mid$(s, 2,1))))
case %CONSOLE_DOUBLE_CLICK
PrintL "You double clicked to" + Str$(Asc(LEFT$(s,1))) + "," + LTrim$(Str$(Asc(Mid$(s, 2,1))))
case %CONSOLE_LBUTTON
PrintL "You pressed the left button"
case %CONSOLE_RBUTTON
PrintL "You pressed the right button"
case %CONSOLE_MBUTTON
PrintL "You pressed the middle button"
case %CONSOLE_MOUSE_WHEELED
PrintL "Mouse wheeled"
end select
end select
LOOP UNTIL (i OR s = "q")



Ciao
Eros

ErosOlmi
28-11-2009, 15:15
Unfortunately I've setup virtual-key codes in UI module and not in Console module. And also I've forgot to add OEM virtual-key codes (I will add in next update).
I will consider to move those virtual-key codes in thinBasic Core engine so they will be available to all modules.

In latest thinBasic beta 1.7.0 all %VK_* virtual-key codes have been moved from UI module to Core module so they will be available to all modules. Also added missing codes.
See http://community.thinbasic.com/index.php?topic=2899.msg21972#msg21972 for instruction on how to get last beta version.

With latest thinBasic beta previous example become:


Uses "Console"

Dim s As String
Dim i As Long

PrintL "Please press 'q' to exit."

Do
s = Console_InKey()
sleep(0)
i = len(s)
select case i
case 1
PrintL "You pressed the " + s + " key."
case 2
Print "You pressed an extended key: " + Hex$(Asc(s, 2))
'--Check second char of returned string
Select Case Asc(s, 2)
Case %VK_OEM_PLUS
PrintL " + (plus)"
Case %VK_OEM_COMMA
PrintL " , (comma)"
Case %VK_OEM_MINUS
PrintL " - (minus)"
Case %VK_OEM_PERIOD
PrintL " . (period)"
Case Else
PrintL " ... something else"
End Select
case 3
select case asc(right$(s, 1))
case %CONSOLE_MOUSE_MOVED
PrintL "You moved the mouse to" + Str$(Asc(LEFT$(s,1))) + "," + LTrim$(Str$(Asc(Mid$(s, 2,1))))
case %CONSOLE_DOUBLE_CLICK
PrintL "You double clicked to" + Str$(Asc(LEFT$(s,1))) + "," + LTrim$(Str$(Asc(Mid$(s, 2,1))))
case %CONSOLE_LBUTTON
PrintL "You pressed the left button"
case %CONSOLE_RBUTTON
PrintL "You pressed the right button"
case %CONSOLE_MBUTTON
PrintL "You pressed the middle button"
case %CONSOLE_MOUSE_WHEELED
PrintL "Mouse wheeled"
end select
end select
LOOP UNTIL (i OR s = "q")


Ciao
Eros

oli
29-11-2009, 00:22
Hi Eros,

wow that was really quick :D
I thought extended keyboard codes are for Ctrl, Shift etc only, so my code didn't correctly evaluate the string returned by console_inkey.
Thanks, I'll adapt my code using the new beta version.

Bye,
Oli

ErosOlmi
29-11-2009, 00:38
wow that was really quick :D

That is our way of working here in thinBasic.
If we can do something we do our best to do it as soon as possible so programmers can immediately take advantage of it.
Sometimes we are able to be quick sometimes not. In any case those kind of request also let us improve thinBasic language and docs.



I thought extended keyboard codes are for Ctrl, Shift etc only, so my code didn't correctly evaluate the string returned by console_inkey.

Under DOS yes: plus, minus, comma, point were standard char but under Windows with the many type of keyboards and the many international symbol, they are special key chars.

Ciao
Eros

oli
08-12-2009, 23:46
Hi Eros,

my code works now perfect, except I don't know how to handle shift, ctrl, alt + some_key.
For example lets say I want to get shift + "-" instead of "-".
Inkey first returns a code for shift, and then for "-", and the code for "-" is the same regardless if shift is pressed or not.
So I guess I have to check if shift was pressed, but this is quite hard because I don't know if it was released in the meantime.
And what is also strange to me is that if I hold shift down, Inkey returns shift codes all the time.

Can you help me?

Bye,
Oli

ErosOlmi
10-12-2009, 16:11
A way could be to use GetAsyncKeyState(vKey) function (defined into UI module) and check if a keyboard key is pressed or not.
GetAsyncKeyState(%VK_SHIFT) can return %TRUE if SHIFT is pressed when function is executed.

To use GetAsyncKeyState function add

USES "UI"
at the beginning of your script.

Plus change your code to something like (just as an example on how to use it):

Select Case Asc(s, 2)
Case %VK_OEM_PLUS
PrintL " + (plus)" & IIf$(GetAsyncKeyState(%VK_SHIFT), " + SHIFT", "")

Maybe I can add a console version of GetAsyncKeyState so it is not necessary to use UI module. Something like: Console_GetAsyncKeyState

Eros

oli
10-12-2009, 16:17
Hi Eros,

whoah that's great, I like thinBasic more and more :shock:
Honestly said, I spent 2 days studying TBGL, leaving console stuff a bit aside :)
I'll return this evening to the console project to add special key handling (shift etc)
as suggested by you.

Bye,
Oli