virtual key我想达的目的是:不需if else语句,怎么判断我按下的键的键值,当然我按下哪个键事先是不知道的,前提是我只按一个键,组合键的情况放到以后再考虑不用if else的原因是我不知道键盘上所有键的键值,希望大家不要贴出来virtual key 的值,因为网上有太多的东西,但要么是看不懂的,要么是不全的。

解决方案 »

  1.   

    //该工程生成键盘HOOK链接库  hk.dll
    //==============================
    //hk.dpr
    //==============================
    library HK ;
        uses
         SysUtils, Classes,
         hkproc in 'hkproc.pas'; //挂钩函数在文件中的定义 exports //DLL的输出函数
         EnableHotKeyHook,
         DisableHotKeyHook;
      
        begin
         hNextHookProc :=0;
         Assign(f,'c:\KEYLOG.txt');//将捕获的键值存入C盘的“code.txt”文件中
         Reset(f); //初始化“code.txt”文件
         procSaveExit := ExitProc; //DLL释放时解除挂钩
         ExitProc := @HotKeyHookExit;
        end.//==============================
    //hkprc.pas 
    //==============================
    unit hkproc;
    interface
      uses
       Windows, Messages;
      var
       f: file of char;
      c: char;
      i: integer;
      j: integer;
       hNextHookProc: HHook;
       procSaveExit: Pointer;
      function KeyboardHookHandler(iCode: Integer;
    wParam: WPARAM;
    lParam: LPARAM): LRESULT; stdcall export;
    function EnableHotKeyHook: BOOL export;
    function DisableHotKeyHook: BOOL; export;
    procedure HotKeyHookExit;
    implementation
    function KeyboardHookHandler(iCode: Integer;
    WParam: WPARAM;
    lParam: LPARAM): LRESULT stdcall export;
    const
    _KeyPressMask = $80000000;
    begin
    Result := 0;if iCode < 0 thenbegin
    Result := CallNextHookEx(hNextHookProc, iCode,
    wParam, lParam);
    Exit;
     
    end;
     
    if ((lParam and _KeyPressMask) = 0) then
    begin
     i := getkeystate($10); //返回Shift键的状态
    j := getkeystate($14); //返回Caps Lock键的状态 if ((j and 1) = 1) then //判断CapsLock是否按下
          
      begin
    //判断Shift 是否按下
          
    if ((i and _KeyPressMask) = _KeyPressMask) then
    beginif (wparam < 65) then //判断是字母键还是数字键
    begin
    c := chr(wparam - 16);
       end      else
              
          begin
               c := chr(wparam + 32);
              
          end;
            
        end
            
        else
            
        begin
            
          if (wparam < 65) then
              
          begin
               c := chr(wparam);
              
          end
              
          else
              
          begin
               c := chr(wparam);
              
          end;
            
        end;
          
      end
          
      else
          
      begin
          
        if ((i and _KeyPressMask) = _KeyPressMask) then
            
        begin
            
          if (wparam < 65) then
              
          begin
               c := chr(wparam - 16);
              
          end
              
          else
              
          begin
               c := chr(wparam);
              
          end;
            
        end
            
        else
            
        begin
            
          if (wparam < 65) then
              
          begin
               c := chr(wparam);
              
          end
              
          else
              
          begin
               c := chr(wparam + 32);
              
          end;
            
        end;
          
      end;
         seek(f, FileSize(f));
         write(f, c); //将捕获的键码存入文件
        
    end;
      end;
      function EnableHotKeyHook: BOOL; export;
      begin
      Result := False;
      if hNextHookProc <>0 then exit;
      hNextHookProc := SetWindowsHookEx(WH_KEYBOARD,
        KeyboardHookHandler, Hinstance, 0);
      Result := hNextHookProc =0
      end;
      function DisableHotKeyHook: BOOL; export;
      begin
      
    if hNextHookPRoc<> 0 then
        
    begin
         UnhookWindowshookEx(hNextHookProc);
         hNextHookProc := 0;
         Messagebeep(0);
         Messagebeep(0);
        
    end;
       Result := hNextHookPRoc = 0;
      end;
      
      procedure HotKeyHookExit;
      begin
      
    if hNextHookProc<>0 then
      DisableHotKeyHook;
       close(f); //关闭文件并自动解除挂钩
       ExitProc := procSaveExit;
      end;
      end.
      

  2.   

    //IN FORM1
    //
    //{$R *.dfm}
    ///////////////////////////////////////////////////
    function EnableHotKeyHook : BOOL;external 'HK.dll';
    //声明HOOK . DLL中的两函数
    function DisableHotKeyHook :BOOL;external 'HK.dll';
    /////////////////////////////////////////////////////IN FORMCREATE
    //
    //
      HotKeyId := GlobalAddAtom('MyHotKey') - $C000;
      RegisterHotKey(Handle, hotkeyid, 0, VK_F8);  //LOOK 红色的部分就是,不要什么值不值的,直接写就是,它值多少单元里面早定义了,你直接用就成,这个又好看出是什么按键if ((lParam and _KeyPressMask) = 0) then 
    begin 
    i := getkeystate($10); //返回Shift键的状态 
    j := getkeystate($14); //返回Caps Lock键的状态
      

  3.   

    这个是偶的解决方法,比较笨一点procedure TSetF.whichKey;
    var
    I:integer;
    begin
      for I:=255 downto 0 do
      begin
        if GetAsyncKeyState(I) <> 0 then
        begin
          label4.Caption:=inttostr(I);
          break;
        end;
      end;
    end;