Sign in to follow this  
Followers 0
Celtic88

Help !! ToUnicodeEx!

4 posts in this topic

I try to Translates the specified virtual-key code and keyboard state to the corresponding Unicode character or characters  May my code does not function please help me.

$keycode = 90 ;z

Local $Retu
$lpKeyState = DllStructCreate("byte[256]")

$m1 = _User32_MapVirtualKey($keycode, 0)

$m2 = _User32_GetForegroundWindow()
$m3 = _User32_GetWindowThreadProcessId($m2, 0)
$m4 = _User32_GetKeyboardLayout($m3)

_User32_ToUnicodeEx($keycode, $m1, $lpKeyState, $Retu, 5, 0, $m4)
MsgBox(0, '', $Retu)
$lpKeyState = 0

Func _User32_ToUnicodeEx($wVirtKey, $wScanCode, $lpKeyState, $pwszBuff, $cchBuff, $wFlags, $dwhkl)
    Local $vRetVal = DllCall("user32.dll", "int", "ToUnicodeEx", "UINT", $wVirtKey, "UINT", $wScanCode, "ptr", $lpKeyState, "wstr", $pwszBuff, "int", $cchBuff, "UINT", $wFlags, "hwnd", $dwhkl)
    Return $vRetVal[0]
EndFunc   ;==>_User32_ToUnicodeEx
Func _User32_MapVirtualKey($uCode, $uMapType)
    Local $vRetVal = DllCall("user32.dll", "UINT", "MapVirtualKey", "UINT", $uCode, "UINT", $uMapType)
    Return $vRetVal[0]
EndFunc   ;==>_User32_MapVirtualKey
Func _User32_GetForegroundWindow()
    Local $vRetVal = DllCall("user32.dll", "hwnd", "GetForegroundWindow")
    Return $vRetVal[0]
EndFunc   ;==>_User32_GetForegroundWindow
Func _User32_GetWindowThreadProcessId($hWnd, $lpdwProcessId)
    Local $vRetVal = DllCall("user32.dll", "DWORD", "GetWindowThreadProcessId", "hwnd", $hWnd, "ptr", $lpdwProcessId)
    Return $vRetVal[0]
EndFunc   ;==>_User32_GetWindowThreadProcessId
Func _User32_GetKeyboardLayout($idThread)
    Local $vRetVal = DllCall("user32.dll", "hwnd", "GetKeyboardLayout", "DWORD", $idThread)
    Return $vRetVal[0]
EndFunc   ;==>_User32_GetKeyboardLayout

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Out of curiosity... Why not just the _WinAPI_* functions already done for you?

Worse case, look at how they implemented their code for your functions versus yours.

Edit:

#1.  You are trying to get the value of $pwszBuff exactly how?  You didn't even make the param ByRef.

#2.  You have $pwszBuff setup to receive a wstr, when it states it's a ptr

#3.  Even if you had that setup right, you didn't pass a ptr.

Func _User32_ToUnicodeEx($wVirtKey, $wScanCode, $lpKeyState, ByRef $pwszBuff, $cchBuff, $wFlags, $dwhkl)
    Local $vRetVal = DllCall("user32.dll", "int", "ToUnicodeEx", "UINT", $wVirtKey, "UINT", $wScanCode, "ptr", $lpKeyState, "wstr*", "", "int", $cchBuff, "UINT", $wFlags, "hwnd", $dwhkl)
    If @error Then Return SetError(1, 0, 0)
    $pwszBuff = $vRetVal[4]
    Return $vRetVal[0]
EndFunc   ;==>_User32_ToUnicodeEx

See if that does what you're looking for.

Edited by SmOke_N
1 person likes this

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Out of curiosity... Why not just the _WinAPI_* functions already done for you?

Worse case, look at how they implemented their code for your functions versus yours.

Edit:

#1.  You are trying to get the value of $pwszBuff exactly how?  You didn't even make the param ByRef.

#2.  You have $pwszBuff setup to receive a wstr, when it states it's a ptr

#3.  Even if you had that setup right, you didn't pass a ptr.

Func _User32_ToUnicodeEx($wVirtKey, $wScanCode, $lpKeyState, ByRef $pwszBuff, $cchBuff, $wFlags, $dwhkl)
    Local $vRetVal = DllCall("user32.dll", "int", "ToUnicodeEx", "UINT", $wVirtKey, "UINT", $wScanCode, "ptr", $lpKeyState, "wstr*", "", "int", $cchBuff, "UINT", $wFlags, "hwnd", $dwhkl)
    If @error Then Return SetError(1, 0, 0)
    $pwszBuff = $vRetVal[4]
    Return $vRetVal[0]
EndFunc   ;==>_User32_ToUnicodeEx

See if that does what you're looking for.

 

I tried with your code it may not function

$keycode = 90 ;z

Local $Retu=""
$lpKeyState = DllStructCreate("byte[256]")

$m1 = _User32_MapVirtualKey($keycode, 0)

$m2 = _User32_GetForegroundWindow()
$m3 = _User32_GetWindowThreadProcessId($m2, 0)
$m4 = _User32_GetKeyboardLayout($m3)

$sdfsd=_User32_ToUnicodeEx($keycode, $m1, DllStructGetPtr($lpKeyState), $Retu, 5, 0, $m4)

MsgBox(0, '', $Retu)
$lpKeyState = 0

Func _User32_ToUnicodeEx($wVirtKey, $wScanCode, $lpKeyState, ByRef $pwszBuff, $cchBuff, $wFlags, $dwhkl)
    Local $vRetVal = DllCall("user32.dll", "int", "ToUnicodeEx", "UINT", $wVirtKey, "UINT", $wScanCode, "ptr", $lpKeyState, "wstr*", "", "int", $cchBuff, "UINT", $wFlags, "hwnd", $dwhkl)
    If @error Then Return SetError(1, 0, 0)
    $pwszBuff = $vRetVal[4]
    Return $vRetVal[0]
EndFunc   ;==>_User32_ToUnicodeEx
Func _User32_MapVirtualKey($uCode, $uMapType)
    Local $vRetVal = DllCall("user32.dll", "UINT", "MapVirtualKey", "UINT", $uCode, "UINT", $uMapType)
    Return $vRetVal[0]
EndFunc   ;==>_User32_MapVirtualKey
Func _User32_GetForegroundWindow()
    Local $vRetVal = DllCall("user32.dll", "hwnd", "GetForegroundWindow")
    Return $vRetVal[0]
EndFunc   ;==>_User32_GetForegroundWindow
Func _User32_GetWindowThreadProcessId($hWnd, $lpdwProcessId)
    Local $vRetVal = DllCall("user32.dll", "DWORD", "GetWindowThreadProcessId", "hwnd", $hWnd, "ptr", $lpdwProcessId)
    Return $vRetVal[0]
EndFunc   ;==>_User32_GetWindowThreadProcessId
Func _User32_GetKeyboardLayout($idThread)
    Local $vRetVal = DllCall("user32.dll", "hwnd", "GetKeyboardLayout", "DWORD", $idThread)
    Return $vRetVal[0]
EndFunc   ;==>_User32_GetKeyboardLayout

Share this post


Link to post
Share on other sites

Ah I sui happy :D I found the solution 

SmOke_N

 thank you for your help 

$keycode = 89 ;z

Local $Retu
$lpKeyState = DllStructCreate("byte[256]")

$m1 = _User32_MapVirtualKey($keycode, 0)

$m2 = _User32_GetForegroundWindow()
$m3 = _User32_GetWindowThreadProcessId($m2, 0)
$m4 = _User32_GetKeyboardLayout($m3)

_User32_ToUnicodeEx($keycode, $m1, DllStructGetPtr($lpKeyState), $Retu, 5, 0, $m4)
MsgBox(0, '', $Retu)
$lpKeyState = 0

Func _User32_ToUnicodeEx($wVirtKey, $wScanCode, $lpKeyState, ByRef $pwszBuff, $cchBuff, $wFlags, $dwhkl)
    Local $vRetVal = DllCall("user32.dll", "int", "ToUnicodeEx", "UINT", $wVirtKey, "UINT", $wScanCode, "ptr", $lpKeyState, "wstr", $pwszBuff, "int", $cchBuff, "UINT", $wFlags, "hwnd", $dwhkl)
    $pwszBuff = $vRetVal[4]
    Return $vRetVal[0]
EndFunc   ;==>_User32_ToUnicodeEx
Func _User32_MapVirtualKey($uCode, $uMapType)
    Local $vRetVal = DllCall("user32.dll", "UINT", "MapVirtualKey", "UINT", $uCode, "UINT", $uMapType)
    Return $vRetVal[0]
EndFunc   ;==>_User32_MapVirtualKey
Func _User32_GetForegroundWindow()
    Local $vRetVal = DllCall("user32.dll", "hwnd", "GetForegroundWindow")
    Return $vRetVal[0]
EndFunc   ;==>_User32_GetForegroundWindow
Func _User32_GetWindowThreadProcessId($hWnd, $lpdwProcessId)
    Local $vRetVal = DllCall("user32.dll", "DWORD", "GetWindowThreadProcessId", "hwnd", $hWnd, "ptr", $lpdwProcessId)
    Return $vRetVal[0]
EndFunc   ;==>_User32_GetWindowThreadProcessId
Func _User32_GetKeyboardLayout($idThread)
    Local $vRetVal = DllCall("user32.dll", "hwnd", "GetKeyboardLayout", "DWORD", $idThread)
    Return $vRetVal[0]
EndFunc   ;==>_User32_GetKeyboardLayout

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0