Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to use ToAscii properly

Posted on 2004-08-27
10
Medium Priority
?
681 Views
Last Modified: 2008-01-09
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.
0
Comment
Question by:psybustermk3
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 7

Expert Comment

by:Jenn3
ID: 11918889
Maybe you could showing the example...
0
 

Author Comment

by:psybustermk3
ID: 11919604
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.vkCode, 0), VarPtr(keybstate), VarPtr(keychar), 0)

I think I'm doing something wrong with the pointers but I can't figure out what.
0
 

Author Comment

by:psybustermk3
ID: 11921860
Increasing point.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:nichia
ID: 11922739
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.vkCode, 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")
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 11922897
It might help to show the ToAscii declaration (which I have found using the API viewer) and the KBDLLHOOKSTRUCT definition, which I can't.
0
 
LVL 3

Expert Comment

by:nichia
ID: 11922946
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!
0
 
LVL 3

Accepted Solution

by:
nichia earned 390 total points
ID: 11922999
Oops!  That declare is for 16 bit windows.  Try using this declare:

Declare Function ToAscii Lib "user32" (ByVal wVirtKey As Long, ByVal wScanCode As Long, lpKeyState As Any, lpChar As Any, Byval wFlags As Long) As Long
0
 

Author Comment

by:psybustermk3
ID: 11923366
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.
0
 

Author Comment

by:psybustermk3
ID: 11942719
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.vkCode, 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.

0
 

Author Comment

by:psybustermk3
ID: 11943106
I forgot to change the code. My mistake.

released = ToAscii(Hookstruct.vkCode, MapVirtualKey(Hookstruct.vkCode, 0), VarPtr(keybstate), keychar, 0)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question