记得c语言里面可以while(true){getch();}
delphi里面是否必须用线程?还有其它的简单方法吗?
我想实现的功能是若有键盘按下,则执行一函数。而且在该应用程序运行后,窗口发灰(即焦点在别的应用程序上),也可以的。类似于QQ的快捷键呼出。

解决方案 »

  1.   

    >>while(true){getch();}是单线程的,就可这样,如果要在windows 中判断,你可用hook技术,
    WH_KEYBOARD, 参考相关实现
      

  2.   

    你也可以定义个全局热键
    如:你定义 Ctrl+Alt+P 就执行 'c:\pp.exe -cmd1'
    你再在pp.exe程序里根据传来的参数做相应的处理就可以了!
      

  3.   

    关于这方面的问题 网上有好多 不过一般都是采用hook
    但是实现的代码也有 你可以试一试
      

  4.   

    你在窗体的 OnKeyPress 写上你的事件,然后把窗体的 KeyPreview 改为 True 就OK了。
      

  5.   

    定义全局热键就可以了,RegisterHotKey
      

  6.   

    支持 wychero(高天) ,用最簡單的方法,實現 HOOK 同樣的功能.何樂而不為?
      

  7.   

    To: wychero(高天)和sailxia(小帆),那个keyPress属性有什么用处?
    另外,如果这个form失去了焦点,按键按下后,就没有反应。是还需要结合RegisterHotKey函数一起使用吧?
      

  8.   

    回复人: jlee084(编程浪子) ( ) 信誉:100  2004-07-12 15:08:00  得分: 0  
     
     
       如何定义全局热键?感谢
      
     ------------创建 程序的快捷方式,在快捷方式里设即可。
      

  9.   

    看来用钩子应该不错!
    我在葵花宝典里找到一代码,是将按键下进一文件中的。虽然系统经常提示非法操作,也写不进文件,但思路大致可行。是一dll。
    我现在打算将该代码改造一下,实现的目的就是:当有按键按下时,主程序能知道!请大家指点一下,谢谢。本人不懂钩子,手头的书里面也没有记载。关键代码:
    function KeyboardHookHandler(iCode : Integer;   WParam : WPARAM;   lParam : LPARAM) : LRESULT ;stdcall ; export;  const   _KeyPressMask = $80000000 ;  begin   Result :=0;   if iCode <0 then   begin   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   begin   if (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;  
      

  10.   

    使用热键,简单的小例子
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs,SConnect, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        procedure Button2Click(Sender: TObject);
        procedure FormClose(Sender: TObject; var Action: TCloseAction);  private
        { Private declarations }
         hotkey:Integer;    
        procedure HotKeyDown(var Msg: Tmessage); message WM_HOTKEY;
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button2Click(Sender: TObject);
    begin
      hotkey:=globaladdatom(pchar('UserDefineHotKey'))-$C000;
      if RegisterHotKey(Handle,hotkey,0,VK_F11) then
            application.MessageBox('ok','asd',mb_ok);
    end;procedure TForm1.HotKeyDown(var Msg: Tmessage);
    begin
       if (Msg.LParamHi = VK_F11) then
            showmessage('ok'); 
    end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
            UnRegisterHotKey(handle, HotKey);
    end;end.
      

  11.   

    My God!原来使用热键可以如此简单!!!!
    To:hellenlong(海龙),你虽然不一定是传说总的钩子高手,但绝对是热键高手!!!
      

  12.   

    更簡單的,去下個叫fishotkey的三方控件就OK.http://www.playicq.com 上面有我上傳的,或者我也可以給你Mail過去,幹脆,去我的個人主頁下吧http://freehost19.websamba.com/sail2000/files/FisHotKey.rar這樣隻要配合Hotkey控件,自由的定義自己需要的按鍵了.
      

  13.   

    注册两个以上的热键:
    procedure TForm1.hotykey(var msg:TMessage);
    begin
    {if (msg.LParamHi=key[0]) then
      begin
      //a:='play "F:\ra2\ra2\Taunts\TAUAM03.wav"';
      //PlaySound('F:\ra2\ra2\Taunts\TAUAM03.wav',0,SND_ASYNC);
      mciSendString(pchar(soundStr),'',0,0);
      showmessage('aa');
      end;if (msg.LParamHi=82) then
      begin
      //PlaySound('F:\ra2\ra2\Taunts\TAUAM01.wav',0,SND_ASYNC);
      mciSendString(pchar('play "F:\ra2\ra2\Taunts\TAUAM07.wav"'),'',0,0);
      showmessage('bb');
      end;}adotable1.Open;while not adotable1.Eof do
      begin  if msg.LParamHi=adotable1.fieldbyname('key').AsInteger then
        begin    with adoQuery1 do
          begin
          active:=false;
          sql.Clear ;
          SQL.Add('SELECT * FROM setKey');
          SQL.Add('where key='''+adotable1.fieldbyname('key').AsString+'''');
          Active :=true;
          end;    //a:='play "F:\ra2\ra2\Taunts\TAUAM03.wav"';
        //PlaySound('F:\ra2\ra2\Taunts\TAUAM03.wav',0,SND_ASYNC);
        //showmessage(adoQuery1.fieldbyname('soundFile').AsString);
        soundStr:='play "'+adoQuery1.fieldbyname('soundFile').AsString+'"';
        mciSendString(pchar(soundStr),'',0,0);
        label3.Visible:=true;
        label6.Visible:=true;
        label5.Visible:=true;
        label3.Caption:=adoQuery1.fieldbyname('soundFile').AsString;
        label6.Caption:=adoQuery1.fieldbyname('memo').AsString;
        label5.Caption:=adoQuery1.fieldbyname('key').AsString;
        end;  adotable1.Next ;
      end;adotable1.Close;end;procedure TForm1.FormCreate(Sender: TObject);
    var
      inifile:Tinifile;
      db1url:string;
      i,col1,row1:integer;begin
    inifile:= Tinifile.Create(ExtractFilePath(paramstr(0))+'option.ini');if FileExists(ExtractFilePath(paramstr(0))+'option.ini') then
      begin
      db1url:=inifile.readString('Option','db1url','Provider=Microsoft.Jet.OLEDB.4.0;Password="";Data Source=keyS.mdb;Persist Security Info=True');
      ADOC1.ConnectionString:=db1url;
      end
    else
      begin
      inifile.WriteString('Option','db1url','Provider=Microsoft.Jet.OLEDB.4.0;Password="";Data Source=keyS.mdb;Persist Security Info=True');
      db1url:=inifile.readString('Option','db1url','Provider=Microsoft.Jet.OLEDB.4.0;Password="";Data Source=setS.mdb;Persist Security Info=True');
      ADOC1.ConnectionString:=db1url;
      end;inifile.Free;adotable1.Open;
    i:=0;
    col1:=0;row1:=1;
    stringgrid1.RowCount:=adotable1.RecordCount+1;while not adotable1.Eof do
      begin  //if GlobalfindAtom(pchar(inttostr(adotable1.fieldbyname('id').AsInteger)))=0 then
        //begin
        id[i]:=GlobalAddAtom(pchar(inttostr(adotable1.fieldbyname('id').AsInteger)))-$C000;
        CT:=RegisterHotKey(handle,id[i],0,adotable1.fieldbyname('key').AsInteger);    if CT=false then
          showmessage('热键 '+adotable1.fieldbyname('key').AsString+' 冲突');    stringgrid1.cells[col1,row1+i]:=adotable1.fieldbyname('soundFile').AsString;    case adotable1.fieldbyname('key').AsInteger of
          192:stringgrid1.cells[col1+1,row1+i]:='`';
          49:stringgrid1.cells[col1+1,row1+i]:='1';
          50:stringgrid1.cells[col1+1,row1+i]:='2';
          51:stringgrid1.cells[col1+1,row1+i]:='3';
          52:stringgrid1.cells[col1+1,row1+i]:='4';
          53:stringgrid1.cells[col1+1,row1+i]:='5';
          54:stringgrid1.cells[col1+1,row1+i]:='6';
          55:stringgrid1.cells[col1+1,row1+i]:='7';
          56:stringgrid1.cells[col1+1,row1+i]:='8';
          57:stringgrid1.cells[col1+1,row1+i]:='9';
          48:stringgrid1.cells[col1+1,row1+i]:='0';
          189:stringgrid1.cells[col1+1,row1+i]:='-';
          187:stringgrid1.cells[col1+1,row1+i]:='=';
          220:stringgrid1.cells[col1+1,row1+i]:='\';
          81:stringgrid1.cells[col1+1,row1+i]:='Q';
          87:stringgrid1.cells[col1+1,row1+i]:='W';
          69:stringgrid1.cells[col1+1,row1+i]:='E';
          82:stringgrid1.cells[col1+1,row1+i]:='R';
          84:stringgrid1.cells[col1+1,row1+i]:='T';
          89:stringgrid1.cells[col1+1,row1+i]:='Y';
          85:stringgrid1.cells[col1+1,row1+i]:='U';
          73:stringgrid1.cells[col1+1,row1+i]:='I';
          79:stringgrid1.cells[col1+1,row1+i]:='O';
          80:stringgrid1.cells[col1+1,row1+i]:='P';
          219:stringgrid1.cells[col1+1,row1+i]:='[';
          221:stringgrid1.cells[col1+1,row1+i]:=']';
          65:stringgrid1.cells[col1+1,row1+i]:='A';
          83:stringgrid1.cells[col1+1,row1+i]:='S';
          68:stringgrid1.cells[col1+1,row1+i]:='D';
          70:stringgrid1.cells[col1+1,row1+i]:='F';
          71:stringgrid1.cells[col1+1,row1+i]:='G';
          72:stringgrid1.cells[col1+1,row1+i]:='H';
          74:stringgrid1.cells[col1+1,row1+i]:='J';
          75:stringgrid1.cells[col1+1,row1+i]:='K';
          76:stringgrid1.cells[col1+1,row1+i]:='L';
          186:stringgrid1.cells[col1+1,row1+i]:=';';
          222:stringgrid1.cells[col1+1,row1+i]:='''';
          90:stringgrid1.cells[col1+1,row1+i]:='Z';
          88:stringgrid1.cells[col1+1,row1+i]:='X';
          67:stringgrid1.cells[col1+1,row1+i]:='C';
          86:stringgrid1.cells[col1+1,row1+i]:='V';
          66:stringgrid1.cells[col1+1,row1+i]:='B';
          78:stringgrid1.cells[col1+1,row1+i]:='N';
          77:stringgrid1.cells[col1+1,row1+i]:='M';
          188:stringgrid1.cells[col1+1,row1+i]:=',';
          190:stringgrid1.cells[col1+1,row1+i]:='.';
          191:stringgrid1.cells[col1+1,row1+i]:='/';
          45:stringgrid1.cells[col1+1,row1+i]:='Insert';
          36:stringgrid1.cells[col1+1,row1+i]:='Home';
          33:stringgrid1.cells[col1+1,row1+i]:='PageUp';
          34:stringgrid1.cells[col1+1,row1+i]:='PageDown';
          35:stringgrid1.cells[col1+1,row1+i]:='End';
          111:stringgrid1.cells[col1+1,row1+i]:='Num /';
          106:stringgrid1.cells[col1+1,row1+i]:='Num *';
          109:stringgrid1.cells[col1+1,row1+i]:='Num -';
          107:stringgrid1.cells[col1+1,row1+i]:='Num +';
          103:stringgrid1.cells[col1+1,row1+i]:='Num 7';
          104:stringgrid1.cells[col1+1,row1+i]:='Num 8';
          105:stringgrid1.cells[col1+1,row1+i]:='Num 9';
          100:stringgrid1.cells[col1+1,row1+i]:='Num 4';
          101:stringgrid1.cells[col1+1,row1+i]:='Num 5';
          102:stringgrid1.cells[col1+1,row1+i]:='Num 6';
          97:stringgrid1.cells[col1+1,row1+i]:='Num 1';
          98:stringgrid1.cells[col1+1,row1+i]:='Num 2';
          99:stringgrid1.cells[col1+1,row1+i]:='Num 3';
          96:stringgrid1.cells[col1+1,row1+i]:='Num 0';
          110:stringgrid1.cells[col1+1,row1+i]:='Num Del';
          38:stringgrid1.cells[col1+1,row1+i]:='Up';
          40:stringgrid1.cells[col1+1,row1+i]:='Down';
          37:stringgrid1.cells[col1+1,row1+i]:='Left';
          39:stringgrid1.cells[col1+1,row1+i]:='Right';
        end;    stringgrid1.cells[col1+2,row1+i]:=adotable1.fieldbyname('memo').AsString;
        //end
      //else
        //begin
        //showmessage('热键 '+adotable1.fieldbyname('key').AsString+' 冲突');
        //GlobaldeleteAtom(id[i]);
        //end;  i:=i+1;
      adotable1.Next ;
      end;adotable1.Close;end;procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    var
      i:integer;
    begin
    adotable1.Open;
    i:=0;
    while not adotable1.Eof do
      begin
      UnRegisterHotKey(handle,id[i]);
      GlobaldeleteAtom(id[i]);  i:=i+1;
      adotable1.Next ;
      end;adotable1.Close;end;