如何使用Qt在Windows(全局)中禁用密钥?

时间:2017-08-30 19:56:10

标签: c++ windows qt desktop

是否可以禁用或阻止某个键,例如 Print Screen 键?

我的事件过滤器:

bool EventFilter::eventFilter(QObject *object, QEvent *event)
{
    qDebug() << "object:" << object << "type:" << event->type();
    return false;
}

我尝试在qApp中使用:

ui->setupUi(this);

EventFilter *evt = new EventFilter;

qApp->installEventFilter(evt);

但是只返回应用程序小部件中的事件:

object: QWidgetWindow(0x175bae50, name = "QWidgetClassWindow") type: QEvent::Type(PlatformSurface)
object: QWidget(0x175b02c0) type: QEvent::Type(PlatformSurface)
object: QWidget(0x175b02c0) type: QEvent::Type(WinIdChange)
object: QWidget(0x175b02c0) type: QEvent::Type(Create)
...

ui->setupUi(this);

EventFilter *evt = new EventFilter;

QDesktopWidget *c = new QDesktopWidget;
c->installEventFilter(evt);

但只返回2个事件:

object: QDesktopWidget(0x174e0260, name = "desktop") type: QEvent::Type(PolishRequest)
object: QDesktopWidget(0x174e0260, name = "desktop") type: QEvent::Type(Polish)

无法拦截和/或阻止事件?感谢

1 个答案:

答案 0 :(得分:4)

您可以安装low-level keyboard hook来拦截并阻止所有 Print Screen 按下。以下是在我的Windows 7机器上测试的。

这是一个显示如何执行此操作的最小示例:

ScreenShot

     <?php
        function startForm(){ 
        viewform();
         }



        // view form in herdoc style 
        function viewform() {

          list($stickFormInputs2,$errors) = validateForm();

          if(!empty($errors)){
            foreach($errors as $key => $value ){

          }}



            print<<<_FORM_
                 <form method='POST' action="./form.php">

                <p><input type="text" name="user_name"  value="{$stickFormInputs2['user_name']}" ></p>
                <p><input type="email" name="email"  value="{$stickFormInputs2['email']} " ></p>

                <p> 
                     <label for="" > Subscibe for newsletter ? </label>
                    <input type="checkbox" name="subscribe" value="1" {$stickFormInputs2['subscribe']} />
                </p>

                <p>which country do you have visited ?
                    <input type="checkbox" name="county[]" value="uk"   {$stickFormInputs2['uk'] }/>UK<br />
                    <input type="checkbox" name="county[]" value="usa" {$stickFormInputs2['usa'] } />USA<br />
                    <input type="checkbox" name="county[]" value="uae"  {$stickFormInputs2['uae'] }  />UAE<br />
               </p>



                <p> this  is the drop down list  </p>
                    <p>
                    <select name='major' >
                    <option value='0'>SELECT ONE </option>
                    <option value='cs'  {$stickFormInputs2['cs'] }> CS </option>
                    <option value='cis' {$stickFormInputs2['cis'] }> CIS </option>
                    <option value='sw' {$stickFormInputs2['sw'] }> SW </option>
                    <option value="bis" {$stickFormInputs2['bis'] } > BIS </option>
                       </select>
                </p>



                 <p>
                    which car  you will use ?
                    <input type="radio" name="car" value="bmw" {$stickFormInputs2['bmw'] }>BMW
                    <input type="radio" name="car" value="audi"    {$stickFormInputs2['audi'] }>AUDI
                    <input type="radio" name="car" value="porsh" {$stickFormInputs2['porsh'] }>PORSH
               </p>


                <input type="submit" value="submit">

                </form> 
        _FORM_;
        }


// validate form and return array of inputs and errors 
 function validateForm() {

            $inputs = array();
            $errors = array();
            // defaults values 
            $inputs = array(
                'user_name'=>'',
                 'email'=>'',
                 'subscribe'=>'',
                 'uk'=>'',
                 'usa'=>'',
                 'uae'=>'',
                 'cs'=>'',
                 'cis'=>'',
                 'sw'=>'',
                 'bis'=>'',
                 'bmw'=>'',
                 'audi'=>'',
                'porsh'=>'',
                   );


            if($_SERVER['REQUEST_METHOD']=='POST'){
            // Check user_name
            if (!isset($_POST['user_name']) || strlen(trim($_POST['user_name'])) == 0) {
                $errors[] = ' ::::::::::::::::::::: Enter your name . name Required';
            } else {
                $inputs['user_name']  = htmlentities(strip_tags($_POST['user_name']));

                echo "<br/>user name is OK from ValidatForm() : " . $inputs['user_name'];
            }


            // Check EMAIL
            if (!isset($_POST['email']) || strlen(trim($_POST['email'])) == 0) {
                $errors[] = ' ::::::::::::::::::::: Enter your EMAIL . EMAIL Required';
            } else {

                if (filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL)) {

                    $inputs['email'] =  htmlentities(strip_tags(trim($_POST['email'])));
                    echo "<br/> email is OK from ValidatForm() : {" . $inputs['email'] . "}";
                } else {
                    $errors[] = ' ::::::::::::::::::::: Your Email is Not Valid ! from FormValidate()';
                }
            }


            // Check subscribe check box
            if (!isset($_POST['subscribe']) || empty($_POST['subscribe'] )) {

                $errors[] = " ::::::::::::::::::::: Subscribe is not checked " ;
            } else {
                $inputs['subscribe'] = 'checked';
                echo "<br/>User want to subscribe in our newsletter ValidatForm() : " . $inputs['subscribe'];
            }



            if (isset($_POST['county']) && !empty($_POST['county'])) {

                foreach ($_POST['county'] as $county) {

                    if ($county == "uk") {
                        $inputs['uk'] = "checked";
                    }

                    if ($county == "usa") {
                        $inputs['usa'] = "checked";
                    }

                    if ($county == "uae") {
                        $inputs['uae'] = "checked";
                    }
                }
            } else {
                $errors[] = ' ::::::::::::::::::::: Select Country ! ';
            }




            // Check Dropdown menu
            if (isset($_POST['major']) && !empty($_POST['major'])) {

                switch ($_POST['major']) {
                    case "cs": $inputs['cs'] = "selected";
                        break;
                    case "cis":$inputs['cis'] = "selected";
                        break;
                    case "sw":$inputs['sw'] = "selected";
                        break;
                    case "bis":$inputs['bis'] = "selected";
                        break;
                }
            } else {

                $errors[] = " ::::::::::::::::::::: SELECT FORM DROP DOWN MENU major";
            }





            //chack radio buttons CAR
            if (isset($_POST['car']) && !empty($_POST['car'])) {

                switch ($_POST['car']) {
                    case "bmw": $inputs['bmw'] = "checked";
                        break;
                    case "audi":$inputs['audi'] = "checked";
                        break;
                    case "porsh":$inputs['porsh'] = "checked";
                        break;
                }
            } else {
                $errors[] = " ::::::::::::::::::::: You must choose a car to be for you  in this trip !";
            }

            }

            return array($inputs, $errors);
        }

在安装它的线程中调用钩子。这样做的好处是不必担心回调函数中的线程安全问题,并且32位进程和64位进程之间没有区别(所有进程最终都会通过发布a来间接调用钩子函数消息到安装了挂钩的线程的事件循环。但是,这也有一个缺点,如果您的线程忙(执行其他任何操作),可能不会调用回调挂钩:

  

钩子过程应该在比LowLevelHooksTimeout值中指定的数据条目更短的时间内处理消息...该值以毫秒为单位。如果挂钩过程超时,系统会将消息传递给下一个挂钩。但是,在Windows 7及更高版本中,无需调用即可静默删除挂钩。应用程序无法知道钩子是否被移除。

您可以通过为安装/执行挂钩分配单独的线程来解决此限制。另请注意,钩子的回调函数应尽可能小,以便能够在时间限制内完成。

P.S。:我必须使用剪切工具来创建上面的屏幕截图。 。