diff options
| author | Akshay Nair <phenax5@gmail.com> | 2024-12-26 00:17:53 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2024-12-26 00:20:47 +0530 |
| commit | 6ad789149036a9e97a9c66860828892efa432bd4 (patch) | |
| tree | b509812fc69f1af890c1c4ddbcda79f81bc9ab43 | |
| parent | d9b2256047669b5a5dbac4baec7140f18a5b6eff (diff) | |
| download | chelleport-6ad789149036a9e97a9c66860828892efa432bd4.tar.gz chelleport-6ad789149036a9e97a9c66860828892efa432bd4.zip | |
Add c-hjkl for movement keys
Diffstat (limited to '')
| -rw-r--r-- | README.md | 8 | ||||
| -rw-r--r-- | specs/Specs/AppStateSpec.hs | 20 | ||||
| -rw-r--r-- | src/Chelleport.hs | 14 | ||||
| -rw-r--r-- | src/Chelleport/AppState.hs | 11 | ||||
| -rw-r--r-- | src/Chelleport/Control.hs | 19 | ||||
| -rw-r--r-- | src/Chelleport/Types.hs | 4 |
6 files changed, 57 insertions, 19 deletions
@@ -5,7 +5,7 @@ Control your mouse pointer entirely with your keyboard. ## Modes -- **Labelled Hints mode (default. `ctrl+h`)**: Displays a grid overlay on your screen, where each cell is labeled with a unique two-key combination. Press the corresponding keys to move the cursor to the desired cell. +- **Labelled Hints mode (default. `ctrl+t`)**: Displays a grid overlay on your screen, where each cell is labeled with a unique two-key combination. Press the corresponding keys to move the cursor to the desired cell. - **Text Search mode (`ctrl+s`)**: Uses OCR to identify and highlight words on the screen, allowing you to search for text and move the cursor directly to matching text. @@ -42,15 +42,15 @@ https://github.com/user-attachments/assets/93ddc1ff-6cbe-4be4-9507-d68de880212a ## Usage Use [sxhkd](https://github.com/baskerville/sxhkd), [shotkey](https://github.com/phenax/shotkey), your window manager or any other key binding manager to set up a keybinding for `chelleport`. -### Hints mode (`ctrl-h` to switch to hints mode) +### Hints mode (`ctrl+t` to switch to hints mode) - With the grid open, type any of the key sequences shown on the grid to move the pointer there - Once a match is found, you can now use `hjkl` keys to make smaller movements. Hold `shift` + `hjkl` to move in bigger increments. - Press `space` to click -### Search mode (`ctrl-s` to switch to search mode) +### Search mode (`ctrl+s` to switch to search mode) - Words that are recognized by OCR will be highlighted - Type the characters in one of the words to move the cursor to it -- Press `ctrl-n` & `ctrl-p` to go to next/previous match respectively +- Press `ctrl+n` & `ctrl+p` to go to next/previous match respectively ## Feedback and Support diff --git a/specs/Specs/AppStateSpec.hs b/specs/Specs/AppStateSpec.hs index eb5ab1b..7eeb082 100644 --- a/specs/Specs/AppStateSpec.hs +++ b/specs/Specs/AppStateSpec.hs @@ -175,6 +175,26 @@ test = do ((_, action), _) <- runWithMocks $ update currentState $ HandleKeyInput SDL.KeycodeF action `shouldBe` Just (MoveMousePosition (1640, 370)) + context "with action MoveMouseInDirection" $ do + let currentState = defaultState + + context "when direction is up" $ do + it "continues to increment movement" $ do + ((_, action), _) <- runWithMocks $ update currentState $ MoveMouseInDirection DirUp + action `shouldBe` Just (IncrementMouseCursor (0, -33)) + context "when direction is down" $ do + it "continues to increment movement" $ do + ((_, action), _) <- runWithMocks $ update currentState $ MoveMouseInDirection DirDown + action `shouldBe` Just (IncrementMouseCursor (0, 33)) + context "when direction is left" $ do + it "continues to increment movement" $ do + ((_, action), _) <- runWithMocks $ update currentState $ MoveMouseInDirection DirLeft + action `shouldBe` Just (IncrementMouseCursor (-60, 0)) + context "when direction is right" $ do + it "continues to increment movement" $ do + ((_, action), _) <- runWithMocks $ update currentState $ MoveMouseInDirection DirRight + action `shouldBe` Just (IncrementMouseCursor (60, 0)) + context "with action MoveMousePosition" $ do let currentState = defaultState diff --git a/src/Chelleport.hs b/src/Chelleport.hs index 4b3ed42..be4f3ac 100644 --- a/src/Chelleport.hs +++ b/src/Chelleport.hs @@ -3,8 +3,8 @@ module Chelleport where import Chelleport.AppShell (setupAppShell) import qualified Chelleport.AppState as AppState import Chelleport.Context (initializeContext) -import Chelleport.Control (anyAlphanumeric, anyDigit, checkKey, ctrl, eventToKeycode, key, pressed, released, shift) -import Chelleport.KeySequence (keycodeToInt) +import Chelleport.Control (anyAlphanumeric, anyDigit, checkKey, ctrl, eventToKeycode, hjklDirection, key, pressed, released, shift) +import Chelleport.KeySequence (keycodeToInt, toKeyChar) import Chelleport.Types import Chelleport.Utils ((<||>)) import qualified Chelleport.View @@ -28,7 +28,8 @@ run = do runAppWithCtx ctx = (`runReaderT` ctx) . runAppM eventHandler :: State -> SDL.Event -> Maybe AppAction -eventHandler state event = +eventHandler state event = do + let hjkl = key SDL.KeycodeH <||> key SDL.KeycodeJ <||> key SDL.KeycodeK <||> key SDL.KeycodeL case SDL.eventPayload event of SDL.QuitEvent -> Just ShutdownApp SDL.KeyboardEvent ev @@ -36,11 +37,14 @@ eventHandler state event = | checkKey [key SDL.KeycodeEscape, pressed] ev -> Just ShutdownApp -- <C-s>: Enable search mode | checkKey [ctrl, key SDL.KeycodeS, pressed] ev -> Just $ SetMode defaultSearchMode - -- <C-h>: Enable hints mode - | checkKey [ctrl, key SDL.KeycodeH, pressed] ev -> Just $ SetMode defaultHintsMode + -- <C-t>: Enable hints mode + | checkKey [ctrl, key SDL.KeycodeT, pressed] ev -> Just $ SetMode defaultHintsMode -- <C-n>, <C-p>: Search increment next/prev | checkKey [ctrl, key SDL.KeycodeN, pressed] ev -> Just $ IncrementHighlightIndex (stateRepetition state) | checkKey [ctrl, key SDL.KeycodeP, pressed] ev -> Just $ IncrementHighlightIndex (-1 * stateRepetition state) + -- <C-hjkl>: Movement + | checkKey [ctrl, hjkl, pressed] ev -> + MoveMouseInDirection . hjklDirection <$> toKeyChar (eventToKeycode ev) -- Space / Shift+Space : Left click/chain left click | checkKey [key SDL.KeycodeSpace, pressed] ev -> if shift ev diff --git a/src/Chelleport/AppState.hs b/src/Chelleport/AppState.hs index 25575f1..7687257 100644 --- a/src/Chelleport/AppState.hs +++ b/src/Chelleport/AppState.hs @@ -1,7 +1,7 @@ module Chelleport.AppState (initialState, update) where import Chelleport.AppShell (MonadAppShell (hideWindow, showWindow, shutdownApp)) -import Chelleport.Control (MonadControl (..), directionalIncrement) +import Chelleport.Control (MonadControl (..), directionalIncrement, hjklDirection) import Chelleport.Draw (MonadDraw (windowPosition, windowSize), pointerPositionIncrement, screenPositionFromCellPosition, wordPosition) import Chelleport.KeySequence (findMatchPosition, generateGrid, nextChars, toKeyChar) import Chelleport.OCR (MonadOCR (captureScreenshot), getWordsInImage) @@ -36,9 +36,7 @@ update state@(State {stateMode = ModeHints}) (HandleKeyInput keycode) = do case (toKeyChar keycode, validNextKeys) of (Just keyChar, Just validChars') | stateIsMatched state && keyChar `elem` ("HJKL" :: String) -> do - incr <- pointerPositionIncrement state - let action = IncrementMouseCursor $ directionalIncrement incr keyChar - pure (state, Just action) + pure (state, Just $ MoveMouseInDirection $ hjklDirection keyChar) | keyChar `elem` validChars' -> do let newKeySequence = stateKeySequence state ++ [keyChar] let matchPosition = findMatchPosition newKeySequence $ stateGrid state @@ -115,6 +113,11 @@ update state MouseDragToggle | stateIsDragging state = pure (state {stateIsDragging = False}, Just MouseDragEnd) | otherwise = do pure (state {stateIsDragging = True}, Just MouseDragStart) +-- Apply movement in given direction +update state (MoveMouseInDirection direction) = do + incr <- pointerPositionIncrement state + pure (state, Just $ IncrementMouseCursor $ directionalIncrement incr direction) + -- Move mouse to given position update state (MoveMousePosition (x, y)) = do moveMousePointer (intToCInt x) (intToCInt y) diff --git a/src/Chelleport/Control.hs b/src/Chelleport/Control.hs index 48bdcb2..e7bd418 100644 --- a/src/Chelleport/Control.hs +++ b/src/Chelleport/Control.hs @@ -94,10 +94,17 @@ anyDigit = isKeycodeDigit . eventToKeycode anyAlphanumeric :: SDL.KeyboardEventData -> Bool anyAlphanumeric = isValidKey . eventToKeycode -directionalIncrement :: (CInt, CInt) -> Char -> (Int, Int) +hjklDirection :: Char -> Direction +hjklDirection = \case + 'H' -> DirLeft + 'L' -> DirRight + 'K' -> DirUp + 'J' -> DirDown + _ -> DirUp + +directionalIncrement :: (CInt, CInt) -> Direction -> (Int, Int) directionalIncrement (incX, incY) = \case - 'H' -> (-cIntToInt incX, 0) - 'L' -> (cIntToInt incX, 0) - 'K' -> (0, -cIntToInt incY) - 'J' -> (0, cIntToInt incY) - _ -> undefined + DirLeft -> (-cIntToInt incX, 0) + DirRight -> (cIntToInt incX, 0) + DirUp -> (0, -cIntToInt incY) + DirDown -> (0, cIntToInt incY) diff --git a/src/Chelleport/Types.hs b/src/Chelleport/Types.hs index f526fe8..89b2e3d 100644 --- a/src/Chelleport/Types.hs +++ b/src/Chelleport/Types.hs @@ -62,6 +62,9 @@ defaultAppState = stateMode = ModeHints } +data Direction = DirUp | DirDown | DirLeft | DirRight + deriving (Show, Eq) + data AppAction = ChainMouseClick MouseButtonType | HandleKeyInput SDL.Keycode @@ -70,6 +73,7 @@ data AppAction | MouseDragEnd | MouseDragStart | MouseDragToggle + | MoveMouseInDirection Direction | MoveMousePosition (Int, Int) | ResetKeys | SetMode Mode |
