Virtual key press [Resolved]

Just starting out? Need help? Post your questions and find answers here.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Virtual key press [Resolved]

Post by Kwai chang caine »

Hello at all

I have found all alone (finally i hope :oops:) how simulate a key press for the API ToUnicode_() and ToAscii_() 8)
That works for the SHIFT key but not for the "ALT GR" (Right ALT) :|
After searching a long time i have found, that if i give this two value 128 / 129 in decimal that works

Code: Select all

; WORKS VERY WELL
Key(#VK_CONTROL) = 128  ; Virtual VK_CONTROL pressed
Key(#VK_MENU) = 129  ; Virtual VK_MENU pressed
but not with & %10000000 like the SHIFT :shock:

Code: Select all

; NOT WORKS
Key(#VK_CONTROL) & %10000000 ; Virtual VK_CONTROL pressed
Key(#VK_MENU) & %10000000 ; Virtual VK_MENU pressed
and i don't understand why :oops:

If someone have understand why

Code: Select all

Global Dim Key.a(256)

Procedure GetInfos(Character$)
  
 Ascii_ByAsc = Asc(Character$)
 VkCode = VkKeyScan_(Ascii_ByAsc) & $FF
 
 ToAscii_(VkCode, MapVirtualKey_(VkCode, 0), @Key(), @Ascii_ByToAscii.w, 0)
 ToUnicode_(VkCode, MapVirtualKey_(VkCode, 0), @Key(), @Ascii_ByToUnicode, SizeOf(Ascii_ByToUnicode) / SizeOf(character) - 1,0)
 
 Debug "Character$ = " + Character$
 
 Debug "Ascii_ByAsc = " + Ascii_ByAsc
 Debug "Chr(Ascii_ByAsc) = " + Chr(Ascii_ByAsc)
 
 Debug "Ascii_ByToAscii = " + Ascii_ByToAscii
 Debug "Chr(Ascii_ByToAscii) = " + Chr(Ascii_ByToAscii)
  
 Debug "Ascii_ByToUnicode = " + Ascii_ByToUnicode
 Debug "Chr(Ascii_ByToUnicode) = " + Chr(Ascii_ByToUnicode)
 
 Debug #CRLF$ + RSet("", 20, "#") + #CRLF$

EndProcedure

Debug "Try LCASE" + #CRLF$
GetInfos("a")

Debug "Try UCASE" + #CRLF$
Key(#VK_SHIFT) & %10000000 ; Virtual SHIFT pressed
GetInfos("A")

Dim Key.a(256)
Debug "Try ALTGR (Right ALT) WORKS VERY WELL" + #CRLF$
Key(#VK_CONTROL) = 128  ; Virtual VK_CONTROL pressed
Key(#VK_MENU) = 129  ; Virtual VK_MENU pressed
GetInfos("$")

Dim Key.a(256)
Debug "Try ALTGR (Right ALT) NOT WORKS" + #CRLF$
Key(#VK_CONTROL) & %10000000 ; Virtual VK_CONTROL pressed
Key(#VK_MENU) & %10000000 ; Virtual VK_MENU pressed
GetInfos("$")
Have a good day
Last edited by Kwai chang caine on Sat Jan 28, 2023 9:14 pm, edited 1 time in total.
ImageThe happiness is a road...
Not a destination
User avatar
bgeraghty
User
User
Posts: 52
Joined: Wed Apr 02, 2014 12:45 am
Location: irc.ibotched.it:+6697
Contact:

Re: Virtual key press

Post by bgeraghty »

Hi Kwai,
I have been working on writing some keyboard input stuff myself lately so it is fresh in my mind, maybe I can help or learn something myself.

My use may be a little different, I'm keeping global track of modifier keys being up/down like CTRL/ALT/SHIFT/WIN and doing it at the keydown/keyup level with hooks. Also I have used this macro which helps in some other spots, but again I'm not 100% clear what you are building this as part of or what it is to accomplish.

Code: Select all

Macro bKeyDown(key):Bool(GetAsyncKeyState_(key)&$8000):EndMacro
In your code I turned on Explicit, and made a couple variable declarations in the procedure so it was happy, and this is the output I get.

Code: Select all

EnableExplicit
Global Dim Key.a(256)

Procedure GetInfos(Character$)
  
 Protected Ascii_ByAsc = Asc(Character$)
 Protected VkCode = VkKeyScan_(Ascii_ByAsc) & $FF
 Protected Ascii_ByToAscii.w, Ascii_ByToUnicode
 ToAscii_(VkCode, MapVirtualKey_(VkCode, 0), @Key(), @Ascii_ByToAscii, 0)
 ToUnicode_(VkCode, MapVirtualKey_(VkCode, 0), @Key(), @Ascii_ByToUnicode, SizeOf(Ascii_ByToUnicode) / SizeOf(character) - 1,0)
 
 Debug "Character$ = " + Character$
 
 Debug "Ascii_ByAsc = " + Ascii_ByAsc
 Debug "Chr(Ascii_ByAsc) = " + Chr(Ascii_ByAsc)
 
 Debug "Ascii_ByToAscii = " + Ascii_ByToAscii
 Debug "Chr(Ascii_ByToAscii) = " + Chr(Ascii_ByToAscii)
  
 Debug "Ascii_ByToUnicode = " + Ascii_ByToUnicode
 Debug "Chr(Ascii_ByToUnicode) = " + Chr(Ascii_ByToUnicode)
 
 Debug #CRLF$ + RSet("", 20, "#") + #CRLF$

EndProcedure

Debug "Try LCASE" + #CRLF$
GetInfos("a")

Debug "Try UCASE" + #CRLF$
Key(#VK_SHIFT) & %10000000 ; Virtual SHIFT pressed
GetInfos("A")

Dim Key.a(256)
Debug "Try ALTGR (Right ALT) WORKS VERY WELL" + #CRLF$
Key(#VK_CONTROL) = 128  ; Virtual VK_CONTROL pressed
Key(#VK_MENU) = 129  ; Virtual VK_MENU pressed
GetInfos("$")

Dim Key.a(256)
Debug "Try ALTGR (Right ALT) NOT WORKS" + #CRLF$
Key(#VK_CONTROL) & %10000000 ; Virtual VK_CONTROL pressed
Key(#VK_MENU) & %10000000 ; Virtual VK_MENU pressed
GetInfos("$")
Output:

Code: Select all

Try LCASE

Character$ = a
Ascii_ByAsc = 97
Chr(Ascii_ByAsc) = a
Ascii_ByToAscii = 97
Chr(Ascii_ByToAscii) = a
Ascii_ByToUnicode = 97
Chr(Ascii_ByToUnicode) = a

####################

Try UCASE

Character$ = A
Ascii_ByAsc = 65
Chr(Ascii_ByAsc) = A
Ascii_ByToAscii = 97
Chr(Ascii_ByToAscii) = a
Ascii_ByToUnicode = 97
Chr(Ascii_ByToUnicode) = a

####################

Try ALTGR (Right ALT) WORKS VERY WELL

Character$ = $
Ascii_ByAsc = 36
Chr(Ascii_ByAsc) = $
Ascii_ByToAscii = 0
Chr(Ascii_ByToAscii) = 
Ascii_ByToUnicode = 0
Chr(Ascii_ByToUnicode) = 

####################

Try ALTGR (Right ALT) NOT WORKS

Character$ = $
Ascii_ByAsc = 36
Chr(Ascii_ByAsc) = $
Ascii_ByToAscii = 52
Chr(Ascii_ByToAscii) = 4
Ascii_ByToUnicode = 52
Chr(Ascii_ByToUnicode) = 4

####################
Mine has the debugs blank on the one that says it works, so I'm not sure what is expected to be seen. Maybe a little more info will help me understand if I'm missing a point.
SolveMyIssue_() - No QuickHelp available.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Virtual key press

Post by Kwai chang caine »

Hello bgeraghty :D

I have tested your code and infortunately thats the same thing
The last result are always $ then it must be ¤ like the previous result :|
Try LCASE

Character$ = a
Ascii_ByAsc = 97
Chr(Ascii_ByAsc) = a
Ascii_ByToAscii = 97
Chr(Ascii_ByToAscii) = a
Ascii_ByToUnicode = 97
Chr(Ascii_ByToUnicode) = a

####################

Try UCASE

Character$ = A
Ascii_ByAsc = 65
Chr(Ascii_ByAsc) = A
Ascii_ByToAscii = 97
Chr(Ascii_ByToAscii) = a
Ascii_ByToUnicode = 97
Chr(Ascii_ByToUnicode) = a

####################

Try ALTGR (Right ALT) WORKS VERY WELL

Character$ = $
Ascii_ByAsc = 36
Chr(Ascii_ByAsc) = $
Ascii_ByToAscii = 164
Chr(Ascii_ByToAscii) = ¤
Ascii_ByToUnicode = 164
Chr(Ascii_ByToUnicode) = ¤

####################

Try ALTGR (Right ALT) NOT WORKS

Character$ = $
Ascii_ByAsc = 36
Chr(Ascii_ByAsc) = $
Ascii_ByToAscii = 36
Chr(Ascii_ByToAscii) = $
Ascii_ByToUnicode = 36
Chr(Ascii_ByToUnicode) = $

####################
Furthermore it's difficult to works with members who have nearly all the US keyboard
That add a complexity in this affair :|

But thanks when even for your answer and the fix of the Enable explicit problem :wink:
And happy to know you :wink:
ImageThe happiness is a road...
Not a destination
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Virtual key press

Post by infratec »

Code: Select all

Dim Key.a(256)
Debug "Try ALTGR (Right ALT)" + #LF$
Key(#VK_CONTROL) = 128
Key(#VK_LCONTROL) = 128
Key(#VK_MENU) = 129
Key(#VK_RMENU) = 128  
GetInfos("q")
Results in:
Try ALTGR (Right ALT)

Character$ = q
Ascii_ByAsc = 113
Chr(Ascii_ByAsc) = q
Ascii_ByToAscii = 64
Chr(Ascii_ByToAscii) = @
Ascii_ByToUnicode = 64
Chr(Ascii_ByToUnicode) = @

####################
AltGr + q on a german keyboard results in @

Your:

Code: Select all

Debug "Try UCASE" + #CRLF$
Key(#VK_SHIFT) & %10000000 ; Virtual SHIFT pressed
GetInfos("A")
Is nonsense.
Key(#VK_SHIFT) is 0 if you binary AND this with %10000000 it is still 0
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Virtual key press

Post by Kwai chang caine »

Hello INFRATEC :D

I have not really understand your answer :oops:

For me, your code give that
Try ALTGR (Right ALT)

Character$ = q
Ascii_ByAsc = 113
Chr(Ascii_ByAsc) = q
Ascii_ByToAscii = 0
Chr(Ascii_ByToAscii) =
Ascii_ByToUnicode = 0
Chr(Ascii_ByToUnicode) =

####################
But it's surely normal, because German keyboard is not the same than French :wink:

In regards to the $ character i try to use
It's because on the french keyboard i must use ALTGR for have ¤
Image

During a test with other code, i see for have the ¤ character, i not really understand why, but i must use #VK_CONTROL+#VK_RMENU
It's strange because with the keyboard only the ALTGR (#VK_RMENU) it's done :shock:

You say to me
Key(#VK_SHIFT) & %10000000 ; Virtual SHIFT pressed
is not the solution and i believe you, but it's strange because that works with all the letters Lcase()/Ucase(), with also the punctuation, if that need SHIFT
Again a coincidence and strange behavior of the programing :|

The real question is, what i must enter in the array Key() for simulate what the GetKeyboardState_(@Key()) write ?
I have see the only values writing is 128 / 129 / 1 but only the 128/129 are apparently really usefull

Or in other words, can i use ToAscii_() or ToUnicode_() if i not use GetKeyboardState_() and want enter myself if the SHIFT/ALT/CTRL is pressed ?
It's for convert the VkCode in Ascii of a character of a sentence without using the keyboard :idea:
ImageThe happiness is a road...
Not a destination
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Virtual key press

Post by infratec »

Have you tried my code and replaced the 'q' with your '$' :?:
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Virtual key press

Post by Kwai chang caine »

Excuse me...i have'nt understand you ask to me that :oops:
Your code works, but mine too with only
Key(#VK_CONTROL) = 128
Key(#VK_MENU) = 129
My question is other, i'm again bad explain, excuse me again :oops:

My question is why i must use 128 /129 ? when ? where ?
How found this value ? i not understand this history of bits

Code: Select all

 & %10000000)
 & $FF
  >> 8
  etc .... 
There are surely a rapport between this 128/ 129 value and something, but what ?
GetKeyboardState_(@Key()) not writing this 128 / 129 by random :cry:
And why not other value, like 1 / 100 / 255 etc ????
until now, there was always a reason for it to be such and such a value, and this time I don't see why.
I just know the value but not the reason or origin :cry:

And the worst, why Key(#VK_CONTROL) must be 128 and #VK_MENU must be 129 and not the opposite for exampler ?
Why it don't have the same value, or 128 or 129 ? :shock:

It's all this questions who turn into my little head :oops:
ImageThe happiness is a road...
Not a destination
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Virtual key press

Post by infratec »

It is exactly written on the page I linked you:
https://learn.microsoft.com/en-us/windo ... boardstate
When the function returns, each member of the array pointed to by the lpKeyState parameter contains status data for a virtual key. If the high-order bit is 1, the key is down; otherwise, it is up. If the key is a toggle key, for example CAPS LOCK, then the low-order bit is 1 when the key is toggled and is 0 if the key is untoggled.
In my english:
The highest bit (%10000000 = $80 = d128) is set when the key is pressed.
The lowest bit (%00000001 = $01 = d1) is set when it is a toggle key and it is toggled.

So a 'toggle key' can have 4 states:
%00000000 = $00 = d0 -> key not pressed and not toggled
%00000001 = $01 = d1 -> key not pressed and toggled
%10000000 = $80 = d128 -> key pressed an not toggled
%10000001 = $81 = d129 -> key pressed an toggled

Normal keys can only have 2 states:
%00000000 = $00 = d0 -> key not pressed
%10000000 = $80 = d128 -> key pressed
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Virtual key press

Post by Kwai chang caine »

Thanks a lot INFRATEC for your lon explanation 8)
I have understand now why 128 /129 :oops:
And also this history of Toggle key :D

Thanks to your precious explanation i have now modified my

Code: Select all

Key(#VK_CONTROL) & %10000000 ; Virtual VK_CONTROL pressed
Key(#VK_MENU) & %10000000 ; Virtual VK_MENU pressed
in

Code: Select all

Key(#VK_CONTROL) = %10000000 ; Virtual VK_CONTROL pressed
Key(#VK_MENU) = %10000000 ; Virtual VK_MENU pressed
and that works
Again thanks a lot MASTER for delete all this question of my head :wink:

Image

Have the best night of the world 8) , even after helped me all the day :oops:
ImageThe happiness is a road...
Not a destination
Post Reply