我想将虚拟密钥转换为WideString。
到目前为止,这就是我所拥有的...

function VKeytoWideString (Key : Word) : WideString;
var
 WBuff         : array [0..255] of WideChar;
 KeyboardState : TKeyboardState;
 UResult       : Integer;
begin
 GetKeyBoardState (KeyboardState);
 UResult := ToUnicode(key, MapVirtualKey(key, 0), KeyboardState, WBuff, 0,0);
 Result  := WBuff;
 case UResult of
  0 : Result := '';
  1 : SetLength (Result, 1);
  2 :;
  else
   Result := '';
 end;
end;


它总是返回0,但是为什么呢?
请帮忙。

最佳答案

您将cchBuffToUnicode()参数设置为0,而不是实际的缓冲区大小,因此该函数无法存储它转换的任何字符。

尝试以下方法:

function VKeytoWideString (Key : Word) : WideString;
var
  WBuff         : array [0..255] of WideChar;
  KeyboardState : TKeyboardState;
  UResult       : Integer;
begin
  Result := '';
  GetKeyBoardState (KeyboardState);
  ZeroMemory(@WBuff[0], SizeOf(WBuff));
  UResult := ToUnicode(key, MapVirtualKey(key, 0), KeyboardState, WBuff, Length(WBuff), 0);
  if UResult > 0 then
    SetString(Result, WBuff, UResult)
  else if UResult = -1 then
    Result := WBuff;
end;

关于delphi - Delphi VirtualKey到WideString,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10843429/

10-10 15:52