hmonnier
New Member
Enjoying Life
Posts: 27
|
Post by hmonnier on Jun 12, 2020 9:07:42 GMT -5
I would like to be able to have a small utility that would read and display the keystrokes received from the keyboard/mouse. I an trying to determine what keystrokes are used on an application i use with my cnc machine. For example when i click on one of the buttons, i'd like to know what character sequence is sent to the application. I'd like the display to be decimal ie: the user types 'a', and the display shows '97'. All help will be appreciated.....
|
|
|
Post by mknarr on Jun 12, 2020 9:41:01 GMT -5
You can use ASC
key$="a" print asc(key$)
You can get key$ by using Inkey$ but it must be in a graphic window or graphicbox
|
|
hmonnier
New Member
Enjoying Life
Posts: 27
|
Post by hmonnier on Jun 12, 2020 11:38:18 GMT -5
You can use ASC key$="a" print asc(key$)
You can get key$ by using Inkey$ but it must be in a graphic window or graphicbox I'm looking to get the keystrokes from the keyboard buffer as I click on the application button, so i can see the sequence of bytes.
|
|
|
Post by mknarr on Jun 13, 2020 8:33:59 GMT -5
This will get the key press from the keyboard and is supposed to print the ASCII code but doesn't?
a$="a" 'this works to convert a string to ASCII code print asc(a$)
timer 50, [getkey] 'Ever 50 milliseconds the timer fires wait
[getkey] CallDLL #user32, "GetAsyncKeyState",_VK_RETURN as long,fKeyPress as long 'Get the key press if fKeyPress >0 then 'If a key is pressed a$=str$(fKeyPress) 'Convert the key press to a string print asc(a$) 'This should print the ASCII code of the key press but doesn't???? end if wait
Can anyone help here. What is wrong with the conversion of fKeyPress to a string and the ASCII code.
|
|
|
Post by Rod on Jun 13, 2020 11:47:15 GMT -5
That api call checks the state of one specific key. It tells you if the key is down or has been down since you last checked. So not sure if I’d going to do what you want. Not at my pc right now but it looks to me that you are only checking if the enter key is pressed and you will get a yes or no answer.
|
|
|
Post by mknarr on Jun 13, 2020 17:26:55 GMT -5
Yeah Rod you are correct. In my application I am only looking for the 'Enter' key to be pressed So using the example for Inkey$ I wrote this:
Open "Inkey$ Example" for graphics as #graph print #graph, "when characterInput [fetch]" print #graph,"setfocus" wait
[fetch] key$=Inkey$ if len(key$)=1 then print asc(key$);" "; end if wait
It's not perfect because I have the output printed to the Main window which gets covered by the graphic window. I suppose you can print it to the graphic window instead. If the OP has problems doing that I could re write the code to print directly into the graphic window.
|
|
|
Post by mknarr on Jun 13, 2020 17:53:25 GMT -5
Here is code to print the key presses in the graphics window in ASCII
nomainwin WindowWidth=800 WindowHeight=200 Open "Inkey$ Example" for graphics as #graph print #graph, "when characterInput [fetch]" print #graph,"setfocus" a$="" wait
[fetch] key$=Inkey$ if len(key$)=1 then a=asc(key$) 'Convert key$ to ASCII a$=a$+str$(a) 'Convert a to a string a$=a$+" " 'Add entries and a space to each entry #graph "place 10 20;|"; a$ 'Place and print #graph "flush" 'Keeps the entries from disappearing if the window is minimized. end if wait
Hopefully this will get him started.
|
|
|
Post by Brandon Parker on Jun 13, 2020 20:49:08 GMT -5
Just out of curiosity, what CNC machine (controller) are you using? Just because you click a button on the software that says "a" does not mean that the program sends that over the communication protocol to the controller. Just something to think about ...
{:0)
Brandon Parker
|
|
|
Post by Rod on Jun 14, 2020 10:26:28 GMT -5
Just to say, the op was I think asking to read key codes input to another application. The cnc software which would have focus at the time.
So intercepting those keystrokes is no trivial task. You would need to intercept them and pass them on.
Fortunately we don’t need to do any of this. The cnc manual will tell us what keys to press and so we will already know the key codes needed.
Sending specific key codes to an application is an order of magnitude simpler but still requires we find the application and mimic key presses with api code.
I am not sure if that part is required as we were asked to know which keys were pressed. That’s on the manual.
If it is about reading the command sequence subsequently sent over the serial connection to the cnc machine that is a completely different question which would need something like port monitor or similar software.
|
|
|
Post by Brandon Parker on Jun 14, 2020 17:14:09 GMT -5
Yeah, the question is a little ambiguous. I was really looking at this statement ...
Normally, as we all know, buttons do not generate character sequences for "in-application" control; they cause a jump to a Branch/Subroutine/Function in pretty much every language out there. The thing is, what does that subsequent code do, and how does it interact with the CNC controller. If the user is issuing "keystrokes" to the software then the user "should" know which keys are being pressed so there's no benefit there. The benefit is seeing the traffic between the application and the controller. At least that is my perspective since I have a CNC sitting in my workshop.
I think a little more clarification on what the OP is trying to do is definitely required. Until then, on to the next coding adventure...
{:0)
Brandon Parker
|
|
|
Post by mknarr on Jun 15, 2020 8:02:34 GMT -5
Well no matter what it was an interesting and learning experience for me. I learned that GetAsynckeyState doesn't work the way I thought it does but it works the way I want it in my programs and I learned a bit more about Inkey$.
|
|
zpq
New Member
Posts: 18
|
Post by zpq on Sept 30, 2021 10:22:05 GMT -5
Hi I tried this code. It works OK in RUN but not in DEBUG. Is there a reason? Thanks
nomainwin WindowWidth=800 WindowHeight=200 Open "Inkey$ Example" for graphics as #graph print #graph, "when characterInput [fetch]" print #graph,"setfocus" a$="" wait
[fetch] key$=Inkey$ if len(key$)=1 then a=asc(key$) 'Convert key$ to ASCII a$=a$+str$(a) 'Convert a to a string a$=a$+" " 'Add entries and a space to each entry #graph "place 10 20;|"; a$ 'Place and print #graph "flush" 'Keeps the entries from disappearing if the window is minimized. end if wait
|
|
|
Post by Rod on Sept 30, 2021 11:21:59 GMT -5
In debug you are stepping through or animating the program. But the focus changes because of the debug window. Even if you run > in debug not step or animate, you generally need to click on the program window to move the focus then press your key to get the when character event to fire. When a program runs normally you set the focus to the input window and all such keypresses are actioned immediately. Bit clunkier in debug.
You can set break points by clicking in the left margin of the debug window, now when you run and click the program will stop at the line handling the keypress. Just one way to catch key and mouse click events during debug.
|
|
zpq
New Member
Posts: 18
|
Post by zpq on Sept 30, 2021 11:42:23 GMT -5
if / end if does not work in debug
|
|
|
Post by Walt Decker on Sept 30, 2021 12:12:21 GMT -5
Rather than a graphic control you could use an off-form edit control and monitor the keystrokes directly. There would be no conversion from character code to ansi.
|
|