Celtic88 Posted November 24, 2014 Share Posted November 24, 2014 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 Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted November 24, 2014 Moderators Share Posted November 24, 2014 (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 November 24, 2014 by SmOke_N Celtic88 1 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. Link to comment Share on other sites More sharing options...
Celtic88 Posted November 24, 2014 Author Share Posted November 24, 2014 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 expandcollapse popup$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 Link to comment Share on other sites More sharing options...
Celtic88 Posted November 24, 2014 Author Share Posted November 24, 2014 Ah I sui happy 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 Link to comment Share on other sites More sharing options...
Uten Posted September 1, 2018 Share Posted September 1, 2018 I know this is an old thread, but there are no other that use ToUnicodeEx. I Think this example is a bit more generic than the one above. expandcollapse popup; SOURCE: https://www.autoitscript.com/forum/topic/165853-help-tounicodeex/ ; SOURCE: _WinAPI_GetModuleHandle.au3 from helpfile on _WinAPI_GetModuleHandle ; Modified and test code: Uten #include <GUIConstantsEx.au3> #include <StructureConstants.au3> #include <WinAPIConstants.au3> #include <WinAPISys.au3> #include <WindowsConstants.au3> ;TODO: Change script name accordingly. If @ScriptName = "_keyboard.au3" Then OnAutoItExitRegister("_keyboard_terminate") HotKeySet("{ESC}", "_keyboard_terminate") Local $hMod Global $g_hStub_KeyProc = DllCallbackRegister("_KeyProc", "long", "int;wparam;lparam") $hMod = _WinAPI_GetModuleHandle(0) Global $g_hHook = _WinAPI_SetWindowsHookEx($WH_KEYBOARD_LL, DllCallbackGetPtr($g_hStub_KeyProc), $hMod) Local $h=GUICreate("Close with esc",250,70) GUISetState() While ( GUIGetMsg() <> $GUI_EVENT_CLOSE ) sleep(10) WEnd EndIf ; =========================================================== ; callback function ; =========================================================== Func _KeyProc($nCode, $wParam, $lParam) If $nCode < 0 Then Return _WinAPI_CallNextHookEx($g_hHook, $nCode, $wParam, $lParam) EndIf If $wParam = $WM_KEYUP Then Local $tKEYHOOKS = DllStructCreate($tagKBDLLHOOKSTRUCT, $lParam) Local $Retu Local $ret=_User32_ToUnicodeEx(DllStructGetData($tKEYHOOKS, "vkCode"), _ DllStructGetData($tKEYHOOKS, "scanCode"), _ DllStructGetPtr(_WinAPI_GetKeyboardState()) , _ $Retu, 5,0, _WinAPI_GetKeyboardLayout(_WinAPI_GetDesktopWindow ( ))) If $ret > 0 Then ConsoleWrite("$LLKHF_UP: scanCode - " & DllStructGetData($tKEYHOOKS, "scanCode") & @TAB & "vkCode - " & DllStructGetData($tKEYHOOKS, "vkCode") & "[" & $Retu &"]" & @CRLF) EndIF EndIf Return _WinAPI_CallNextHookEx($g_hHook, $nCode, $wParam, $lParam) EndFunc ;==>_KeyProc Func _keyboard_terminate() _WinAPI_UnhookWindowsHookEx($g_hHook) DllCallbackFree($g_hStub_KeyProc) Exit EndFunc ;==>Cleanup Func _User32_ToUnicodeEx($wVirtKey, $wScanCode, $lpKeyState, ByRef $pwszBuff, $cchBuff, $wFlags, $dwhkl) #cs - Parameters ;Translates the specified virtual-key code and keyboard state to the corresponding Unicode character or characters. ; wVirtKey: Virtual key code to be translated. ; wScanCode: The hardware scan code of the key to be translated ; lpKeyState: Pointer to 256-byte array containing keyb state. ; pwszBuff: ByRef return buffer ; cchBuff: The size of buffer pointed to by pwszBuff ; wFlags: Behaviour of function 0x0 or 0x2 ; dwhkl: Local identifyer to keyboard layout. ; RETURN: -1 : Dead key ; 0 : No translation - ex arrow keys ; 1 : 1 char in pwszBuff ; >2 : 2 or more chars in pwszBuff ; ;USAGE: Local $ret=_User32_ToUnicodeEx(DllStructGetData($tKEYHOOKS, "vkCode"), _ DllStructGetData($tKEYHOOKS, "scanCode"), _ DllStructGetPtr(_WinAPI_GetKeyboardState()) , _ $Retu, 5,0, _WinAPI_GetKeyboardLayout(_WinAPI_GetDesktopWindow ( ))) #ce 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 Remember to change the file test under ToDo: Please keep your sig. small! Use the help file. Search the forum. Then ask unresolved questions :) Script plugin demo, Simple Trace udf, TrayMenuEx udf, IOChatter demo, freebasic multithreaded dll sample, PostMessage, Aspell, Code profiling Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now