我试图为一个编辑应用程序实现ctrl-x/v (不是文本,而是屏幕上描述的东西,所以我不能只使用浏览器复制/粘贴)。
所有这些都可以用于以下设置:
model.ctrlPressed toTrue (在KeyCode 17上)然而,有几次我可以按下ctrl按钮时,我点击离开,然后KeyUp从来没有传递给榆树和model.ctrlPressed卡在错误的状态。
因此,我尝试了PageVisibility库,并在Hidden订阅时将ctrlPressed设置为False。如果我将浏览器或开关选项卡降到最低限度,但对于当我单击dev控制台时持有ctrl的实例,这会有所帮助。
也许这是一个只有在开发中才会发生的错误,但我不想冒这个风险。有人有办法解决这个问题吗?
发布于 2017-05-15 16:01:09
你想要的document.hasFocus(). --它不会触发一个事件,但你必须进行投票。
下面是一个示例(跑):
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方面:
var app = Elm.Main.fullscreen();
app.ports.focusStateRequest.subscribe(function() {
var hasFocus = document.hasFocus();
app.ports.focusStateResponse.send(hasFocus);
});https://stackoverflow.com/questions/43808656
复制相似问题