是否可以使用ask功能检测esc键?

发布于 2024-10-03 20:56:47 字数 117 浏览 4 评论 0原文

想要检测 esc 键以逃避伪代码中的永远循环:

forever [ 网址:询问“网址:” 如果(网址= esc)[ 休息 ] ]

这可能吗?

Would like to detect esc key to escape the forever loop in pseudo code:

forever [
url: ask "Url: "
if (url = esc) [
break
]
]

Is this possible ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月蹉跎了容颜 2024-10-10 20:56:47

没有简单的答案,您必须使用自己的控制台端口来正确处理它,这是取自我的旧项目之一的部分内容:

REBOL [title: "Console port"]

set 'ctx-console make object! [
    system/console/busy: none
    system/console/break: false
    buffer: make string! 512
    history: system/console/history
    prompt: "## " ;system/console/prompt
    spec-char: none
    port: none
    init: func[][
        port: open/binary [scheme: 'console]
        set 's-print get in system/words 'print
        set 's-prin  get in system/words 'prin
        set 'prin func[msg /err /inf /user user-data][
            ctx-console/clear-line
            s-prin reform msg
            ctx-console/print-line
        ]
        set 'print func[msg /err /inf /user user-data][
            prin rejoin [reform msg newline]
        ]
        s-prin prompt
    ]
    print-line: func[][
        s-prin rejoin [ prompt head buffer "^(1B)[" length? buffer "D"]
    ]
    clear-line: func[][
        s-prin rejoin [
            "^(1B)[" (
            (index? buffer) +
            (length? prompt) +
            (length? buffer))
            "D^(1B)[K"
        ]
    ]
    key-actions: make block! [
        #{08} [;BACK
            if 0 < length? head buffer [
                buffer: remove back buffer
                s-prin rejoin [
                    "^(back)^(1B)[K"
                    buffer
                    "^(1B)["
                    length? buffer "D"
                ]
            ]
        ]
        #{7E} [;HOME
            s-prin rejoin ["^(1B)[" (index? buffer) - 1 "D"]
            buffer: head buffer
        ]
        #{7F} [;DELETE
            buffer: remove buffer
            s-prin rejoin ["^(1B)[K" buffer "^(1B)[" length? buffer "D"]
        ]
        #{1B} [;ESCAPE
            spec-char: copy/part port 1
            either spec-char = #{1B} [
                print "ESCAPE"
                clear-line
                set 'print :s-print
                set 'prin  :s-prin
                system/console/break: true
                on-escape
            ][
                switch append spec-char copy/part port 1 [
                    #{5B41} [;ARROW UP
                        if not tail? history [
                            clear-line
                            clear head buffer
                            s-prin join prompt buffer: copy history/1
                            history: next history
                            buffer: tail buffer
                        ]
                    ]
                    #{5B42} [;ARROW DOWN
                        clear-line
                        buffer: head buffer
                        clear buffer
                        if all [
                            not error? try [history: back history]
                            not none? history/1
                        ] [
                            buffer: copy history/1
                        ]
                        s-prin join prompt buffer
                        buffer: tail buffer
                    ]
                    #{5B43} [;ARROW RIGHT
                        if not tail? buffer [
                            s-prin "^(1B)[C"
                            buffer: next buffer
                        ]
                    ]
                    #{5B44} [;ARROW LEFT
                        if 1 < index? buffer [
                            s-prin "^(1B)[D"
                            buffer: back buffer
                        ]
                    ]
                ]
            ]
        ]
    ]
    do-command: func[comm /local e][
        set/any 'e attempt compose [do (comm)]

        if all [
            not unset? 'e
            value? 'e
            not object? :e
            not port? :e
            not function? :e
        ][
            print head clear skip rejoin [system/console/result mold :e] 127
            if (length? mold :e) > 127 [
                print "...^/"
            ]
        ]
    ]
    on-enter: func[input-str /local e][
        print rejoin [system/console/prompt input-str]
        do-command input-str
    ]
    on-escape: func[][halt]
    process: func[/local ch c tmp spec-char err][
        ch: to-char pick port 1
        either (ch = newline) or (ch = #"^M") [;ENTER
            tmp: copy head buffer
            if empty? tmp [return none]
            history: head history
            if any [empty? history tmp <> first history ] [
                insert history tmp
            ]
            clear-line
            buffer: head buffer
            clear buffer
            print-line
            on-enter tmp
        ][
            switch/default to-binary ch key-actions [
                either tail? buffer [
                    s-prin ch ;either local-echo [ch]["*"]
                ][
                    s-prin rejoin ["^(1B)[@" ch]
                ]
                buffer: insert buffer ch
            ]
        ]
    ]
]
ctx-console/init

;and now do something with your own console:
wait-list: reduce [ctx-console/port]
forever [
    attempt [ready: wait/all wait-list]
    if ready [
        ctx-console/process
    ]
]

您可能会喜欢更改 ctx-console/on-escape< /strong> 和 ctx-console/on-enter 函数。

There is no simple answer, you must use own console-port to handle it correctly, here is the part of it taken from one of my old projects:

REBOL [title: "Console port"]

set 'ctx-console make object! [
    system/console/busy: none
    system/console/break: false
    buffer: make string! 512
    history: system/console/history
    prompt: "## " ;system/console/prompt
    spec-char: none
    port: none
    init: func[][
        port: open/binary [scheme: 'console]
        set 's-print get in system/words 'print
        set 's-prin  get in system/words 'prin
        set 'prin func[msg /err /inf /user user-data][
            ctx-console/clear-line
            s-prin reform msg
            ctx-console/print-line
        ]
        set 'print func[msg /err /inf /user user-data][
            prin rejoin [reform msg newline]
        ]
        s-prin prompt
    ]
    print-line: func[][
        s-prin rejoin [ prompt head buffer "^(1B)[" length? buffer "D"]
    ]
    clear-line: func[][
        s-prin rejoin [
            "^(1B)[" (
            (index? buffer) +
            (length? prompt) +
            (length? buffer))
            "D^(1B)[K"
        ]
    ]
    key-actions: make block! [
        #{08} [;BACK
            if 0 < length? head buffer [
                buffer: remove back buffer
                s-prin rejoin [
                    "^(back)^(1B)[K"
                    buffer
                    "^(1B)["
                    length? buffer "D"
                ]
            ]
        ]
        #{7E} [;HOME
            s-prin rejoin ["^(1B)[" (index? buffer) - 1 "D"]
            buffer: head buffer
        ]
        #{7F} [;DELETE
            buffer: remove buffer
            s-prin rejoin ["^(1B)[K" buffer "^(1B)[" length? buffer "D"]
        ]
        #{1B} [;ESCAPE
            spec-char: copy/part port 1
            either spec-char = #{1B} [
                print "ESCAPE"
                clear-line
                set 'print :s-print
                set 'prin  :s-prin
                system/console/break: true
                on-escape
            ][
                switch append spec-char copy/part port 1 [
                    #{5B41} [;ARROW UP
                        if not tail? history [
                            clear-line
                            clear head buffer
                            s-prin join prompt buffer: copy history/1
                            history: next history
                            buffer: tail buffer
                        ]
                    ]
                    #{5B42} [;ARROW DOWN
                        clear-line
                        buffer: head buffer
                        clear buffer
                        if all [
                            not error? try [history: back history]
                            not none? history/1
                        ] [
                            buffer: copy history/1
                        ]
                        s-prin join prompt buffer
                        buffer: tail buffer
                    ]
                    #{5B43} [;ARROW RIGHT
                        if not tail? buffer [
                            s-prin "^(1B)[C"
                            buffer: next buffer
                        ]
                    ]
                    #{5B44} [;ARROW LEFT
                        if 1 < index? buffer [
                            s-prin "^(1B)[D"
                            buffer: back buffer
                        ]
                    ]
                ]
            ]
        ]
    ]
    do-command: func[comm /local e][
        set/any 'e attempt compose [do (comm)]

        if all [
            not unset? 'e
            value? 'e
            not object? :e
            not port? :e
            not function? :e
        ][
            print head clear skip rejoin [system/console/result mold :e] 127
            if (length? mold :e) > 127 [
                print "...^/"
            ]
        ]
    ]
    on-enter: func[input-str /local e][
        print rejoin [system/console/prompt input-str]
        do-command input-str
    ]
    on-escape: func[][halt]
    process: func[/local ch c tmp spec-char err][
        ch: to-char pick port 1
        either (ch = newline) or (ch = #"^M") [;ENTER
            tmp: copy head buffer
            if empty? tmp [return none]
            history: head history
            if any [empty? history tmp <> first history ] [
                insert history tmp
            ]
            clear-line
            buffer: head buffer
            clear buffer
            print-line
            on-enter tmp
        ][
            switch/default to-binary ch key-actions [
                either tail? buffer [
                    s-prin ch ;either local-echo [ch]["*"]
                ][
                    s-prin rejoin ["^(1B)[@" ch]
                ]
                buffer: insert buffer ch
            ]
        ]
    ]
]
ctx-console/init

;and now do something with your own console:
wait-list: reduce [ctx-console/port]
forever [
    attempt [ready: wait/all wait-list]
    if ready [
        ctx-console/process
    ]
]

You will probably like to change the ctx-console/on-escape and ctx-console/on-enter functions.

甜宝宝 2024-10-10 20:56:47

作为一个纯粹的控制台应用程序,我很确定答案是

esc 用于取消脚本的执行。

您可以禁用 esc 的使用....

 system/console/break: false

....并且 esc 键现在不执行任何操作。

如果您切换到REBOL/VIEW并且愿意使用弹出请求文本框而不是控制台询问行,那么您可以能够使用insert-event-func捕获esc

As a pure console application, I'm pretty sure the answer is no.

esc is used to cancel the execution of the script.

You can disable that use of esc....

 system/console/break: false

....And the esc key now does nothing.

If you switch to REBOL/VIEW and are happy to use a pop up request-text box rather than a console ask line, then you may be able to trap esc using insert-event-func.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文