gaslouk
Full Member
Hi from beautiful Greece.
Posts: 130
|
inkey$
Feb 2, 2023 6:15:25 GMT -5
Post by gaslouk on Feb 2, 2023 6:15:25 GMT -5
Hi.
I would like to ask about the use of the "Inkey$" command when after using, What peculiarities does the command have? The more examples you give me the better it will be for me. Many thanks to the team in advance for this and other questions I will ask in the future.
|
|
|
Post by tsh73 on Feb 2, 2023 6:39:23 GMT -5
Inkey$ is not a command
it is a build-in variable, which gets set then key handler (characterInput) got called It works only in graphicbox/graphic window so mainwin programs are out in the could, could not do that to decide if loop should be continued Only then (given graphicbox/graphic window has focus (could be set from a program by "setfocus")) and (given graphicbox/graphic window has key down handler)
And to read key your program should ibe in a WAIT state or in a loop with SCAN opeartor (which allows events to be processed) Having busy loop for 10 second without SCAN will block all events including reading keys.
Inkey$ returns string of 1 or 2 characters ordinary keys return ASCII code special keys return chr$(0)+chr$(someCode) There is a table of codes like _VK_LEFT which LB recognises, but it still numbers 1..255 Help says first character could allow to read SHIFT ALT CTRL (though Alt usually intercepted by Windows)
*I might be wrong but I remember that* ENTER make handler fire twice, . once with chr$(13) . and once with chr$(0)+chr$(13) ESC key fires trice, I think . chr$(27) . chr$(0)+chr$(27) . chr$(27)
|
|
|
Post by Rod on Feb 2, 2023 7:51:10 GMT -5
Use this code, move the graphic window so you can see the mainwin. Now press some keys. The keyboard sends messages which Liberty receives in Inkey$.
Press enter, notice there are two messages. Press backspace, two again, That's because Windows issues messages when these and some other special keys are pressed and again when released. Now try Ctrl on its own then Ctrl together with another key. You will see that there are two parts to the message, the first indicates whether any secondary keys like shift, ctrl or alt are pressed and the second part tells you the main key pressed. So you can differentiate between Ctrl+X or X.
'INKEY.BAS - how to use the Inkey$ variable open "Inkey$ example" for graphics as #graph #graph, "when characterInput [fetch]" #graph, "setfocus" wait
[fetch] 'a character was typed! key$ = Inkey$ if len(key$)=1 then print "Inkey$ length = ";len(key$);" "; print "ASC value = ";asc(key$);" "; print "Glyph character is ";key$ end if if len(key$)=2 then print "Inkey$ length = ";len(key$);" "; print "First ASC value = ";asc(left$(key$,1));" "; print "Second ASC value = ";asc(right$(key$,1)) if key$=chr$(0)+chr$(16) then print "Shift released" end if wait
|
|
|
Post by xxgeek on Feb 2, 2023 16:22:42 GMT -5
Here is an example where the virtual keys are used. This was taken from a larger program and is just a demo here.
The demo just loads a window (a small window) and then sits in a loop waiting for input. The input goes to the textbox. The virtual keys also help with navigation within the textbox. (This was something Rod helped me with in the JB forums)
The reason I did not use this was the virtual keys can be used for more than one result. I forget now which key(s) got in my way, but I would for example press maybe a function key and that would produce something like the letter "p" or "q" in the textbox. I'm sure there is a way around this, but at the time I decided to change my method instead of working it out.
The demo will show what I mean if you press some of the function keys. I needed the function keys for other things and didn't want them typing into the textbox at the time. Someday I will revisit it and figure it out.
Hope this helps with your Inkey$ query.
'Inkey$ Demo nomainwin TITLE$ = "Inkey$ Demo" WindowWidth = 260 : WindowHeight = 70 UpperLeftX=INT((DisplayWidth-WindowWidth)/2) UpperLeftY=INT((DisplayHeight-WindowHeight)/2-300)
'Create the form - open the window - set some fonts textbox #main.tb 0, 18, 165, 20 button #main.go, "&GO", [startSearching], UL, 170, 18, 35, 20 button #main.help, "&Help", [startSearching], UL, 210, 18, 40, 20 checkbox #main.jbhelp, "H", [startSearching], [startSearching], 2, 2, 30, 15 checkbox #main.fform, "F", [startSearching], [startSearching], 37, 2, 25, 15 checkbox #main.tutorial, "T", [startSearching], [startSearching], 67, 2, 25, 15 checkbox #main.tuttext, "TT", [startSearching], [startSearching], 97, 2, 35, 15 checkbox #main.tutcode, "CT", [startSearching], [startSearching], 137, 2, 35, 15 checkbox #main.jbexamples, "CE", [startSearching], [startSearching], 177, 2, 35, 15 checkbox #main.jbfunctions, "CF", [startSearching], [startSearching], 217, 2, 35, 15 open TITLE$ for graphics_nsb_nf as #main #main "trapclose [quit]" #main, "when characterInput [displayKeysPressed]"
[waitinForInput] #main "setfocus"' input hiddenCharacters$ searchForT$ = searchForT$;lastKeyPressed$
[displayKeysPressed] lastKeyPressed$ = right$(Inkey$,1) keyPressed$ = right$(Inkey$,1) keyPressed = asc(right$(keyPressed$, 1)) scan if keyPressed = _VK_Menu then #main "setfocus" : goto [displayKeysPressed] If keyPressed = _VK_Control then #main "setfocus" : goto [displayKeysPressed] if keyPressed = _VK_Tab then #main "setfocus" : goto [displayKeysPressed] if keyPressed$ = "*" and resultsOpen = 1 then [defFont] if keyPressed$ = "-" and resultsOpen = 1 then [decFont] if keyPressed$ = "+" and resultsOpen = 1 then [incFont] if keyPressed = _VK_Shift then [displayKeysPressed] if keyPressed = _VK_Escape and resultsOpen = 1 then resultsOpen = 0 : close #results : keyPressed = _VK_Back : keyPressed$ = "" : Inkey$ = "" : lastKeyPressed$ = "" : searchForT$ = "" if keyPressed = _VK_Escape then keyPressed = _VK_BACK if keyPressed = _VK_Return then [startSearching] if keyPressed = _VK_BACK then searchForT$=left$(searchForT$,len(searchForT$)-1) else searchForT$ = searchForT$;keyPressed$ if keyPressed = _VK_Tab then #main "setfocus" #main.tb searchForT$ if keyPressed$= "." then [startSearching] goto [waitinForInput]
[startSearching] notice "This is just a demo" wait
[quit] close #main end
|
|