2017-05-05 29 views
0

我試圖實現ctrl-x/v編輯應用程序(而不是文本,但屏幕上描繪的東西,所以我不能只使用瀏覽器複製/粘貼)。處理Ctrl + <...>超出基礎的鍵碼

這一切都與以下安裝工作:

  • 在keyDown消息 - 由鍵盤觸發庫 - 設置model.ctrlPressed爲True(在邀請碼17)
  • 一個KEYUP處理器來扭轉這一點。

但是,已經發生一些時間,我可以按住Ctrl鍵,當我點擊即可,然後KeyUp不會傳遞至榆樹和model.ctrlPressed卡在了錯誤的狀態。

所以我嘗試了PageVisibility庫和 - Hidden訂閱 - 我設置ctrlPressed爲False。如果我最小化瀏覽器或切換選項卡,那麼這會有所幫助,但對於單擊開發控制檯時按住ctrl鍵的實例,這不起作用。

也許這是一個只會在開發過程中發生的錯誤,但我不想冒這個風險。任何人都有一個解決方法的建議?

回答

0

你想要document.hasFocus()雖然它不會觸發事件,但您必須爲此進行輪詢。

下面是一個例子(run):

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); 
}); 
相關問題