首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >处理基础以外的Ctrl+<...> KeyCodes

处理基础以外的Ctrl+<...> KeyCodes
EN

Stack Overflow用户
提问于 2017-05-05 15:25:57
回答 1查看 56关注 0票数 0

我试图为一个编辑应用程序实现ctrl-x/v (不是文本,而是屏幕上描述的东西,所以我不能只使用浏览器复制/粘贴)。

所有这些都可以用于以下设置:

  • 由键盘库触发的KeyDown消息设置model.ctrlPressed toTrue (在KeyCode 17上)
  • 一个KeyUp处理程序来逆转这一点。

然而,有几次我可以按下ctrl按钮时,我点击离开,然后KeyUp从来没有传递给榆树和model.ctrlPressed卡在错误的状态。

因此,我尝试了PageVisibility库,并在Hidden订阅时将ctrlPressed设置为False。如果我将浏览器或开关选项卡降到最低限度,但对于当我单击dev控制台时持有ctrl的实例,这会有所帮助。

也许这是一个只有在开发中才会发生的错误,但我不想冒这个风险。有人有办法解决这个问题吗?

EN

回答 1

Stack Overflow用户

发布于 2017-05-15 16:01:09

你想要的document.hasFocus(). --它不会触发一个事件,但你必须进行投票。

下面是一个示例():

代码语言:javascript
复制
port module Main exposing (..)

import Html as H exposing (Html)
import Time


port focusStateRequest : () -> Cmd msg


port focusStateResponse : (Bool -> msg) -> Sub msg


type alias Model =
    { windowFocused : Bool }


type Msg
    = FocusStateRequest
    | FocusStateResponse Bool


main : Program Never Model Msg
main =
    H.program
        { init = init
        , update = update
        , view = view
        , subscriptions = subscriptions
        }


init : ( Model, Cmd Msg )
init =
    ( { windowFocused = True }
    , Cmd.none
    )


update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        FocusStateRequest ->
            ( model
            , focusStateRequest ()
            )

        FocusStateResponse isFocused ->
            -- reset your ctrlPressed here if False
            ( { model | windowFocused = isFocused }
            , Cmd.none
            )


view : Model -> Html Msg
view model =
    model
        |> toString
        |> H.text


subscriptions : Model -> Sub Msg
subscriptions model =
    Sub.batch
        [ Time.every (500 * Time.millisecond) (\_ -> FocusStateRequest)
        , focusStateResponse FocusStateResponse
        ]

在JS方面:

代码语言:javascript
复制
var app = Elm.Main.fullscreen();

app.ports.focusStateRequest.subscribe(function() {
  var hasFocus = document.hasFocus();
  app.ports.focusStateResponse.send(hasFocus);
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43808656

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档