Forum Discussion

hisense's avatar
hisense
Occasional Contributor
5 years ago
Solved

How to block keyboard when script running?

How to block keyboard when script running?
  • Hi,

     

    Does this help?

    (Note: ages old DelphiScript code that might not work in modern TestComplete and/or Windows and I think that you definitely must consider bitness as described here: https://support.smartbear.com/testcomplete/docs/testing-with/advanced/using-external-functions/calling-from-dll/specifics-of-32-and-64-bit-dlls.html)

     
    //-------------------------------------------------------------------------------
    // Blocks keyboard and mouse input events from reaching applications.
    // Function's effect can be cancelled by pressing Ctrl-Alt-Del
    procedure StopKeyMouse;
    var Def_DLL;
    var Def_Proc;
    var Lib;
    begin
        Def_DLL := DLL.DefineDLL('USER32');
        Def_Proc := Def_DLL.DefineProc('BlockInput', vt_b1, vt_b1);
        Lib := DLL.Load('USER32.DLL', 'USER32');
        Lib.BlockInput(true);
    end;
    //-------------------------------------------------------------------------------
     
    // Unblocks keyboard and mouse input events from reaching applications.
    procedure ResumeKeyMouse;
    var Def_DLL;
    var Def_Proc;
    var Lib;
    begin
        Def_DLL := DLL.DefineDLL('USER32');
        Def_Proc := Def_DLL.DefineProc('BlockInput', vt_b1, vt_b1);
        Lib := DLL.Load('USER32.DLL', 'USER32');
        Lib.BlockInput(false);
    end;
    //-------------------------------------------------------------------------------
     

10 Replies

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi,

     

    Does this help?

    (Note: ages old DelphiScript code that might not work in modern TestComplete and/or Windows and I think that you definitely must consider bitness as described here: https://support.smartbear.com/testcomplete/docs/testing-with/advanced/using-external-functions/calling-from-dll/specifics-of-32-and-64-bit-dlls.html)

     
    //-------------------------------------------------------------------------------
    // Blocks keyboard and mouse input events from reaching applications.
    // Function's effect can be cancelled by pressing Ctrl-Alt-Del
    procedure StopKeyMouse;
    var Def_DLL;
    var Def_Proc;
    var Lib;
    begin
        Def_DLL := DLL.DefineDLL('USER32');
        Def_Proc := Def_DLL.DefineProc('BlockInput', vt_b1, vt_b1);
        Lib := DLL.Load('USER32.DLL', 'USER32');
        Lib.BlockInput(true);
    end;
    //-------------------------------------------------------------------------------
     
    // Unblocks keyboard and mouse input events from reaching applications.
    procedure ResumeKeyMouse;
    var Def_DLL;
    var Def_Proc;
    var Lib;
    begin
        Def_DLL := DLL.DefineDLL('USER32');
        Def_Proc := Def_DLL.DefineProc('BlockInput', vt_b1, vt_b1);
        Lib := DLL.Load('USER32.DLL', 'USER32');
        Lib.BlockInput(false);
    end;
    //-------------------------------------------------------------------------------
     
    • hisense's avatar
      hisense
      Occasional Contributor
      Thanks,Alex,it helped.You provide anthoer way to block input.For now I am debuging this code in TC,thanks again!
  • cunderw's avatar
    cunderw
    Community Hero

    Short answer you can't. Test complete sends real keystrokes and mouse actions when playing back.

    • hisense's avatar
      hisense
      Occasional Contributor
      I do NOT think so.Without keyboard,the scripts can work normally.Any guy can show a code sample?Thanks advance!
      • tristaanogre's avatar
        tristaanogre
        Esteemed Contributor

        Right, without a physical keyboard hooked up, it will still work.  However, TestComplete "takes over" the UI input on the operating system while it's executing so any keystrokes ore mouse actions done while the automation is running will interfere.

  • hisense's avatar
    hisense
    Occasional Contributor
    I do NOT think so.Without keyboard,the scripts can work normally.Any guy can show a code sample?Thanks advance!