![Avatar of psybustermk3](https://cdn.experts-exchange.com/images/experts-exchange/avatar-01-large.gif)
psybustermk3
asked on
How to use ToAscii properly
I'm trying to use the ToAscii function. I keep trying to follow examples I found on the net but whenever I try them, I always get an overflow error.
![Avatar of Jenn3](https://filedb.experts-exchange.com/files/public/2013/11/18/f180f6e4-8d3b-4b58-a80c-a87e5fe05176.png)
Maybe you could showing the example...
![Avatar of psybustermk3](https://cdn.experts-exchange.com/images/experts-exchange/avatar-01-large.gif)
ASKER
Ok. Sorry about that.
Private Type KeyboardBytes
kbByte(0 To 255) As Byte
End Type
Private Type KeyCaptured
kchByte(0 To 1) As Byte
End Type
Dim Hookstruct As KBDLLHOOKSTRUCT
Dim keybstate As KeyboardBytes
Dim keychar As KeyCaptured
released = ToAscii(Hookstruct.vkCode, MapVirtualKey(Hookstruct.v kCode, 0), VarPtr(keybstate), VarPtr(keychar), 0)
I think I'm doing something wrong with the pointers but I can't figure out what.
Private Type KeyboardBytes
kbByte(0 To 255) As Byte
End Type
Private Type KeyCaptured
kchByte(0 To 1) As Byte
End Type
Dim Hookstruct As KBDLLHOOKSTRUCT
Dim keybstate As KeyboardBytes
Dim keychar As KeyCaptured
released = ToAscii(Hookstruct.vkCode,
I think I'm doing something wrong with the pointers but I can't figure out what.
![Avatar of psybustermk3](https://cdn.experts-exchange.com/images/experts-exchange/avatar-01-large.gif)
ASKER
Increasing point.
![Avatar of nichia](https://cdn.experts-exchange.com/images/experts-exchange/avatar-01-large.gif)
It's always a good idea to first assign local variables with the values before passing them to functions. It makes for easier debugging and error handling.
The third parameter in ToAscii() wants to see a byte value (lpbKeyState As Byte). You are assigning it the return value of VarPtr() which is a long. This is the source of the error.
This is easier to read, but it still contains the error:
On Error GoTo errhandler
Dim Released As Long
Dim lVirtKey As Long, lScanCode As Long, lpbKeyState As Byte, lpwTransKey As Long
lVirtKey = Hookstruct.vkCode
lScanCode = MapVirtualKey(Hookstruct.v kCode, 0)
lpbKeyState = VarPtr(keybstate)
lpwTransKey = VarPtr(keychar)
Released = ToAscii(lVirtKey, lScanCode, lpbKeyState, lpwTransKey, 0)
Exit Sub
errhandler:
Call MsgBox("Error: " & Err.Number & " : " & Err.Description, vbCritical, "Error Message")
The third parameter in ToAscii() wants to see a byte value (lpbKeyState As Byte). You are assigning it the return value of VarPtr() which is a long. This is the source of the error.
This is easier to read, but it still contains the error:
On Error GoTo errhandler
Dim Released As Long
Dim lVirtKey As Long, lScanCode As Long, lpbKeyState As Byte, lpwTransKey As Long
lVirtKey = Hookstruct.vkCode
lScanCode = MapVirtualKey(Hookstruct.v
lpbKeyState = VarPtr(keybstate)
lpwTransKey = VarPtr(keychar)
Released = ToAscii(lVirtKey, lScanCode, lpbKeyState, lpwTransKey, 0)
Exit Sub
errhandler:
Call MsgBox("Error: " & Err.Number & " : " & Err.Description, vbCritical, "Error Message")
![Avatar of GrahamSkan](https://filedb.experts-exchange.com/files/public/2014/10/4/01f34e8f-ab9d-4b6a-a820-14c156ab295f.jpeg)
It might help to show the ToAscii declaration (which I have found using the API viewer) and the KBDLLHOOKSTRUCT definition, which I can't.
![Avatar of nichia](https://cdn.experts-exchange.com/images/experts-exchange/avatar-01-large.gif)
OK. The declare for ToAscii in the API viewer must be incorrect. A pointer to a byte array cannot be a byte!
I searched for alternative delcares and I found:
Declare Function ToAscii Lib "Keyboard" (ByVal wVirtKey As Integer, ByVal wScanCode As Integer, lpKeyState As Any, lpChar As Any, Byval wFlags As Integer) As Integer
Here, the pointers are declared as Any, meaning they will accept the pointer from VarPtr. See if this works for you.
Be sure to declare lpbKeyState As Long, not Byte if you use my sample code above.
Good Luck!
I searched for alternative delcares and I found:
Declare Function ToAscii Lib "Keyboard" (ByVal wVirtKey As Integer, ByVal wScanCode As Integer, lpKeyState As Any, lpChar As Any, Byval wFlags As Integer) As Integer
Here, the pointers are declared as Any, meaning they will accept the pointer from VarPtr. See if this works for you.
Be sure to declare lpbKeyState As Long, not Byte if you use my sample code above.
Good Luck!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
![Avatar of psybustermk3](https://cdn.experts-exchange.com/images/experts-exchange/avatar-01-large.gif)
ASKER
I tried nichia idea and the errors were gone. But I can't find the output ASCII character which is suppose to be in keychar.
![Avatar of psybustermk3](https://cdn.experts-exchange.com/images/experts-exchange/avatar-01-large.gif)
ASKER
Ok. I made it work by using a long type variable without using varptr.
Private Type KeyboardBytes
kbByte(0 To 255) As Byte
End Type
Private Declare Function ToAscii Lib "user32" (ByVal wVirtKey As Long, ByVal wScanCode As Long, lpKeyState As KeyboardBytes, lpChar As Any, ByVal wFlags As Long) As Long
Dim Hookstruct As KBDLLHOOKSTRUCT
Dim keybstate As KeyboardBytes
Dim keychar As Long
Dim released as Integer
released = ToAscii(Hookstruct.vkCode, MapVirtualKey(Hookstruct.v kCode, 0), VarPtr(keybstate), VarPtr(keychar), 0)
I accept nichia's answer since even if it's not exactly the answer, it's the closest one.
Private Type KeyboardBytes
kbByte(0 To 255) As Byte
End Type
Private Declare Function ToAscii Lib "user32" (ByVal wVirtKey As Long, ByVal wScanCode As Long, lpKeyState As KeyboardBytes, lpChar As Any, ByVal wFlags As Long) As Long
Dim Hookstruct As KBDLLHOOKSTRUCT
Dim keybstate As KeyboardBytes
Dim keychar As Long
Dim released as Integer
released = ToAscii(Hookstruct.vkCode,
I accept nichia's answer since even if it's not exactly the answer, it's the closest one.
![Avatar of psybustermk3](https://cdn.experts-exchange.com/images/experts-exchange/avatar-01-large.gif)
ASKER
I forgot to change the code. My mistake.
released = ToAscii(Hookstruct.vkCode, MapVirtualKey(Hookstruct.v kCode, 0), VarPtr(keybstate), keychar, 0)
released = ToAscii(Hookstruct.vkCode,