Delphi VirtualKey转换为WideString

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


您将ToUnicode()cchBuff参数设置为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; 
链接地址: http://www.djcxy.com/p/61491.html

上一篇: Delphi VirtualKey to WideString

下一篇: Delphi function to convert Result of WrapText to TStringList