For note, if you want to not get HandleKeyPress messages ever it should 
actually be:
```elm

filters : Msg -> Model -> ( Msg, States Model Msg )filters msg model =
  if model.state == Running then
    case msg of
        HandleKeyboardDown c ->
            case Char.fromCode c ->
                'P' -> ( PauseToggle, States.enableAll )
                ' ' -> ( JumpDown, States.enableAll )
                'z' -> ( StartShooting, States.enableAll )
                _ -> ( msg, States.disableAll )
        HandleKeyboardUp c ->
            case Char.fromCode c ->
                ' ' -> ( JumpUp, States.enableAll )
                'z' -> ( StopShooting, States.enableAll )
                _ -> ( msg, States.disableAll )
        _ -> ( msg, States.enableAll )
  else
      case msg of
        HandleKeyboardUp _ -> ( msg, States.disableAll )
        HandleKeyboardDown _ -> ( msg, States.disableAll )
        _ -> ( msg, States.enableAll )

```

Though if you are never handling HandleKeyboardUp/Down in your update it 
does not matter anyway.

On Wednesday, August 10, 2016 at 10:59:40 AM UTC-6, OvermindDL1 wrote:
>
> > If there are alternatives *with currently available functions* to my 
> proposal, very interested to hear it in this thread.
>
> That is why I looked at more options and started thinking about how it 
> could also solve other filtering issues that I have, so a singular thing 
> that could solve it all would be nice.  :-)
>
>
> Given code from the dontfall game, the current top-post suggestion was to 
> change subscriptions to be:
> ```elm
>
> subscriptions : GameData -> Sub GameMsgsubscriptions d =
>     Sub.batch
>         ([ Keyboard.downsSelectively (\c -> if Char.fromCode c == 'P' then 
> Just PauseToogle else Nothing) ] ++
>             if d.state == Running then
>                 [ AnimationFrame.diffs Tick
>                 , Keyboard.downsSelectively (\c -> if Char.fromCode c == ' ' 
> then Just JumpDown else Nothing)
>                 , Keyboard.upsSelectively (\c -> if Char.fromCode c == ' ' 
> then Just JumpUp else Nothing)
>                 ]
>             else
>                 [])
>
> ```
>
> In the thing I proposed that can handle generic message filtering (not 
> just subscription) would turn the above back into a fairly normal and 
> readable style of:
> ```elm
>
> subscriptions : GameData -> Sub GameMsgsubscriptions d =
>     Sub.batch
>       [ Keyboard.downs HandleKeyboardDown
>       , Keyboard.ups HandleKeyboardUps
>       , if d.state == Running then AnimationFrame.diffs Tick else Sub.none
>       ]
>
> ```
> And a new callback (added in the main where init/update/etc are, in my 
> package is called 'filters') would be something like this, this is a very 
> simple example and `filters` can do a lot more, but for simple keyboard 
> filtering:
> ```elm
>
> filters : Msg -> Model -> ( Msg, States Model Msg )filters msg model =
>   if model.state == Running then
>     case msg of
>         HandleKeyboardDown c ->
>             case Char.fromCode c ->
>                 'P' -> ( PauseToggle, States.enableAll )
>                 ' ' -> ( JumpDown, States.enableAll )
>                 _ -> ( msg, States.disableAll )
>         HandleKeyboardUp c ->
>             case Char.fromCode c ->
>                 ' ' -> ( JumpUp, States.enableAll )
>                 _ -> ( msg, States.disableAll )
>         _ -> ( msg, States.enableAll )
>   else
>       ( msg, States.enableAll )
>
> ```
> You could also delegate to other TEA-style modules.  You could filter out 
> the Tick here instead of subscribing and unsubscribing (I like a simple 
> 'subscriptions' callback).  Anything with `States.disableAll` will disable 
> all callbacks for that msg, so update will not be called, subscriptions 
> will not be called, and view will not be called (it returns the cached 
> values from last time so an exact match test of === in javascript shows 
> they are identical and thus Elm will do nothing).
>
> With the above `filters` you will not ever get 
> HandleKeyboardDown/HandleKeyboardUp in your `update` at all, ever, it is 
> either translated to another message or entirely canceled.  Adding more 
> message conversions is as simple as adding another case, so you could add, 
> say a shooting state with like:
> ```elm
>
> filters : Msg -> Model -> ( Msg, States Model Msg )filters msg model =
>   if model.state == Running then
>     case msg of
>         HandleKeyboardDown c ->
>             case Char<span class="pl-k" style="box-sizing: border-box; 
> text-shadow: none !important; box-shadow: none !important; color: rgb(133,
>
>

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to