请问如何检测最后按下的键并执行相应操作?

Post a reply


In an effort to prevent automatic submissions, we require that you complete the following challenge.
Smilies
:D :) ;) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :!: :?: :idea: :| :mrgreen: :geek: :ugeek: :arrow: :angel: :clap: :crazy: :eh: :lolno: :problem: :shh: :shifty: :sick: :silent: :think: :thumbup: :thumbdown: :salute: :wave: :wtf: :yawn: :facepalm: :bravo: :dance: :beard: :morebeard: :xmas: :HeHe: :trollface: :cookie: :rainbow: :monkeysee: :monkeysay: :happybday: :headwall: :offtopic: :superhappy: :terms: :beer:
View more smilies

BBCode is ON
[img] is OFF
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: 请问如何检测最后按下的键并执行相应操作?

Re: 请问如何检测最后按下的键并执行相应操作?

Post by willpangela » 22 Dec 2018, 10:48

tmplinshi wrote:
22 Dec 2018, 09:54
有两个错误。

1. if 语法错误

AHK 中的 if 有两种写法。
  1. 第一种就是你代码中那样,这是 AHK 1.0.x 旧版本遗留下来的写法。

    Code: Select all

    if A_priorkey != lbutton or A_priorkey != enter
    但是这句代码并不是你以为的那样,它的意思其实是:如果 A_priorkey 不等于 lbutton or A_priorkey != enter。可运行以下代码验证一下就知道了:

    Code: Select all

    s := "lbutton or s = enter"
    if s = lbutton or s = enter
    	msgbox 是的,s 等于【lbutton or s = enter】
  2. 第二种写法: if (表达式),这是推荐的写法

    Code: Select all

    if (A_priorkey != "lbutton") or (A_priorkey != "enter")
2. if 逻辑错误

你那句 if 应该改成 and

Code: Select all

#InstallKeybdHook
#InstallmouseHook

sleep 2222

if (A_priorkey != "lbutton") and (A_priorkey != "enter")
	exitapp

msgbox you confirmed

感谢回复

Re: 请问如何检测最后按下的键并执行相应操作?

Post by tmplinshi » 22 Dec 2018, 09:54

有两个错误。

1. if 语法错误

AHK 中的 if 有两种写法。
  1. 第一种就是你代码中那样,这是 AHK 1.0.x 旧版本遗留下来的写法。

    Code: Select all

    if A_priorkey != lbutton or A_priorkey != enter
    但是这句代码并不是你以为的那样,它的意思其实是:如果 A_priorkey 不等于 lbutton or A_priorkey != enter。可运行以下代码验证一下就知道了:

    Code: Select all

    s := "lbutton or s = enter"
    if s = lbutton or s = enter
    	msgbox 是的,s 等于【lbutton or s = enter】
  2. 第二种写法: if (表达式),这是推荐的写法

    Code: Select all

    if (A_priorkey != "lbutton") or (A_priorkey != "enter")
2. if 逻辑错误

你那句 if 应该改成 and

Code: Select all

#InstallKeybdHook
#InstallmouseHook

sleep 2222

if (A_priorkey != "lbutton") and (A_priorkey != "enter")
	exitapp

msgbox you confirmed

请问如何检测最后按下的键并执行相应操作?

Post by willpangela » 21 Dec 2018, 09:17

Code: Select all

#InstallKeybdHook

#InstallmouseHook

sleep 2222

if A_priorkey != lbutton or A_priorkey != enter

    exitapp

msgbox you confirmed


鼠标左击或者按回车提示已确认,不过我写的这个代码不管用,永远都是在if里面就退出了,希望高手指点迷津,谢谢!

Moderator Note: Added code tags. ~ sinkfaze

Top