Skip to content

Commit

Permalink
feat: support long button press (#6)
Browse files Browse the repository at this point in the history
* bind long button presses to new keys

now its possible to exit some apps

* feat: support long press by using shift+key

* docs: button mapping

Co-authored-by: MX <[email protected]>
  • Loading branch information
jon4hz and xMasterX authored Aug 2, 2022
1 parent 3b46911 commit db61d83
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 24 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,22 @@ By default, `fztea` doesn't require any authentication but you can specify an `a
$ fztea server -l 127.0.0.1:2222 -k ~/.ssh/authorized_keys
```

## ⌨️ Button Mapping
| Key | Flipper Event | Keypress Type
|-----------------|---------------|--------------|
| w, ↑ | up | short |
| d, → | right | short |
| s, ↓ | down | short |
| a, ← | left | short |
| o, enter, space | ok | short |
| b, back, esc | back | short |
| W, shift + ↑ | up | long |
| D, shift + → | right | long |
| S, shift + ↓ | down | long |
| A, shift + ← | left | long |
| O | ok | long |
| B | back | long |

## 🎬 Demo


Expand Down
64 changes: 40 additions & 24 deletions flipperzero/flipper.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,16 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.KeyCtrlC:
return nil, tea.Quit
default:
key := mapKey(msg)
key, getlong := mapKey(msg)
if key != -1 {
m.sendFlipperEvent(key)
m.sendFlipperEvent(key, getlong)
}
}

case tea.MouseMsg:
event := mapMouse(msg)
if event != -1 {
m.sendFlipperEvent(event)
m.sendFlipperEvent(event, false)
}

case tea.WindowSizeMsg:
Expand All @@ -100,15 +100,21 @@ func min(a, b int) int {
return b
}

func (m *Model) sendFlipperEvent(event flipper.InputKey) {
func (m *Model) sendFlipperEvent(event flipper.InputKey, isLong bool) {
m.mu.Lock()
defer m.mu.Unlock()
if time.Since(m.lastFZEvent) < fzEventCoolDown {
return
}
m.fz.Flipper.Gui.SendInputEvent(event, flipper.InputTypePress) //nolint:errcheck
m.fz.Flipper.Gui.SendInputEvent(event, flipper.InputTypeShort) //nolint:errcheck
m.fz.Flipper.Gui.SendInputEvent(event, flipper.InputTypeRelease) //nolint:errcheck
if !isLong {
m.fz.Flipper.Gui.SendInputEvent(event, flipper.InputTypePress) //nolint:errcheck
m.fz.Flipper.Gui.SendInputEvent(event, flipper.InputTypeShort) //nolint:errcheck
m.fz.Flipper.Gui.SendInputEvent(event, flipper.InputTypeRelease) //nolint:errcheck
} else {
m.fz.Flipper.Gui.SendInputEvent(event, flipper.InputTypePress) //nolint:errcheck
m.fz.Flipper.Gui.SendInputEvent(event, flipper.InputTypeLong) //nolint:errcheck
m.fz.Flipper.Gui.SendInputEvent(event, flipper.InputTypeRelease) //nolint:errcheck
}
m.lastFZEvent = time.Now()
}

Expand Down Expand Up @@ -154,24 +160,34 @@ func listenScreenUpdate(u <-chan string) tea.Cmd {
}
}

func mapKey(key tea.KeyMsg) flipper.InputKey {
switch key.Type {
case tea.KeyUp:
return flipper.InputKeyUp
case tea.KeyDown:
return flipper.InputKeyDown
case tea.KeyRight:
return flipper.InputKeyRight
case tea.KeyLeft:
return flipper.InputKeyLeft
case tea.KeyEscape:
return flipper.InputKeyBack
case tea.KeyBackspace:
return flipper.InputKeyBack
case tea.KeyEnter, tea.KeySpace:
return flipper.InputKeyOk
func mapKey(key tea.KeyMsg) (flipper.InputKey, bool) {
switch key.String() {
case "w", "up":
return flipper.InputKeyUp, false
case "a", "left":
return flipper.InputKeyLeft, false
case "s", "down":
return flipper.InputKeyDown, false
case "d", "right":
return flipper.InputKeyRight, false
case "o", "enter", " ":
return flipper.InputKeyOk, false
case "b", "backspace", "esc":
return flipper.InputKeyBack, false
case "W", "shift+up":
return flipper.InputKeyUp, true
case "A", "shift+left":
return flipper.InputKeyLeft, true
case "S", "shift+down":
return flipper.InputKeyDown, true
case "D", "shift+right":
return flipper.InputKeyRight, true
case "O":
return flipper.InputKeyOk, true
case "B":
return flipper.InputKeyBack, true
}
return -1
return -1, false
}

func mapMouse(event tea.MouseMsg) flipper.InputKey {
Expand Down

0 comments on commit db61d83

Please sign in to comment.