Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting ASC from WM_KeyDown or WM_KeyUp event?

Status
Not open for further replies.

AndyGroom

Programmer
May 23, 2001
972
GB
I am intercepting the messages from a third party control which doesn't reveal KeyDown, KeyPress or KeyUp events, so that I can replicate those three events by coding them as subroutines. I'm struggling to convert wp (ie. virtual key code) to obtain the correct value for what VB returns as the KeyAscii parameter in the KeyPress event.

Is there a way to convert wp (ie. virtual key code) into an ascii value?

The function below intercepts the Windows messages to the third party control and calls 3 routines called HTMLed1_KeyDown, HTMLed1_KeyPress and HTMLed1_KeyUp. The conversion to an ascii value is flawed, it doesn't work for punctuation for example.

Code:
Private KbdScan(256) As Byte

Public Function HTMLEdMessage(ByVal hwnd As Long, ByVal Msg As Long, ByVal wp As Long, ByVal lp As Long) As Long

  Const WM_KeyDown = 256
  Const WM_KeyUp = 257
  
  Dim Ignore As Boolean
  
  Select Case Msg
    Case WM_KeyDown
      GetKeyboardState KbdScan(0)
      ShiftKeys% = -((KbdScan(16) And 128) = 128) - (2 * ((KbdScan(17) And 128) = 128)) - (4 * ((KbdScan(18) And 128) = 128))
      Form1.HTMLed1_KeyDown CInt(wp), ShiftKeys%
      ' This approach does NOT work:...
      N$ = LCase$(Chr$(wp))
      If ((ShiftKeys% And 1) = 1) Then N$ = UCase$(N$)
      Form1.HTMLed1_KeyPress CInt(Asc(N$))
    
    Case WM_KeyUp
      GetKeyboardState KbdScan(0)
      ShiftKeys% = -((KbdScan(16) And 128) = 128) - (2 * ((KbdScan(17) And 128) = 128)) - (4 * ((KbdScan(18) And 128) = 128))
      Form1.HTMLed1_KeyUp CInt(wp), ShiftKeys%
    
  End Select

  HTMLEdMessage = CallWindowProc(htmled_lngDefaultHandler, hwnd, Msg, wp, lp)
    
End Function

- Andy
___________________________________________________________________
If a man speaks in a forest and there are no women around to hear him - will he still be wrong?
 
Some code of mine from 2003 may help. Check out the vbKeyboardProc function in thread222-716589. If nothing else, look at the MapVirtualKey API call
 
Thanks, I looked at your code and MapVirtualKey but I ran into the same issue when dealing with punctuation.

For better or worse here's my solution, using WM_Char to grab the ASCII value and then passing it on the WM_KeyUp message:

Code:
  Static ASCIIVal As Integer
  
  Select Case Msg
    Case WM_Char
      ASCIIVal = wp
    
    Case WM_KeyUp
      Form1.HTMLed1_KeyPress ASCIIVal
      GetKeyboardState KbdScan(0)
      ShiftKeys% = -((KbdScan(16) And 128) = 128) - (2 * ((KbdScan(17) And 128) = 128)) - (4 * ((KbdScan(18) And 128) = 128))
      Form1.HTMLed1_KeyUp CInt(wp), ShiftKeys%
    
  End Select


- Andy
___________________________________________________________________
If a man speaks in a forest and there are no women around to hear him - will he still be wrong?
 
Should have come back here earlier with a quick demo of that:

dim result as long
GetKeyboardState KbdScan(0)
ToAscii wp, 0&, KbdScan(0), result, 1&
msgbox chr(result)
 
Also check your license agreement.

What you are doing in commonly referred to as "hijacking" a piece of software, and in license agreements it is covered under restrictions regarding reverse engineering and circumvention of provided APIs.

Basically this is usually considered piracy.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top