3

我想将虚拟键转换为 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 但为什么呢?请帮忙。

4

1 回答 1

7

您将cchBuff参数 of设置ToUnicode()为 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; 
于 2012-06-01T01:58:08.413 回答