keepassium / KeePassium

KeePass-compatible password manager for iOS
https://keepassium.com
Other
1.18k stars 105 forks source link

macOS: open entries and copy data using keyboard shortcuts #281

Open latvia234 opened 1 year ago

latvia234 commented 1 year ago

Description After enabling a vault I hit cmd+f and there will appear some entries. When there are entries visible I want to open them with keyboard shortcuts. When hitting the down arrow I can select, but not open the entry. Why does KeePassium behave like this?

Expected behavior

Screenshots Screenshot 2023-03-08 at 21 18 16

keepassium commented 1 year ago

Hi, thanks for the feedback and sorry for the delay.

Why does KeePassium behave like this?

This behavior depends on your macOS version and which KeePassium build you run.

The native Mac version of the app is built using Mac Catalyst. There, keyboard focus/selection depends on macOS version; the app does not handle this at all and I'm not sure if this is even possible without some ~dirty~ fragile low-level tricks.

To complicate things further, it is also possible to run the iOS version of KeePassium on an Apple Silicon Mac. In this case, the system pretends to be an iPad, and handles keyboard events — you guessed it — differently again. And since this is supposed to "just work", the app has little to no control over keyboard-based selection.

I have tested this case on macOS 13.2.1, and the right panel does automatically switch to the currently selected entry.

Can you confirm your macOS version, please?

latvia234 commented 1 year ago

sorry for the delay.

That's no problem!

Can you confirm your macOS version, please?

12.6.3

It seems that it is a better solution to just update to a newer Mac (with a newer macOS).

Edit: accidentally closed.

keepassium commented 1 year ago

12.6.3

Oh, I understand you :) This was my version only a week ago. But debugging on iOS 16.4 requires Xcode 14.3, which requires macOS 13, so here we are…

It seems that it is a better solution to just update to a newer Mac (with a newer macOS).

Most likely, yeah. Though there is no guarantee this won't break again later on :)

latvia234 commented 3 days ago

@keepassium As you probably guessed I'm not a developer – I'm just a regular user of KeePassium. And I love using keyboard shortcuts.

After the addition of several awesome features in 1.53, I hope that you have the time to add some keyboard shortcuts to the macOS version of KeePassium.

Do you think that it is possible to still keep using Catalyst and implement some keyboard shortcuts in the near future?

I know it requires a lot of time to create a macOS-specific version of the app. But maybe there are better options to still keep using Catalyst and implement keyboard shortcuts?

Did you consider the following options?

To address the issue of adding keyboard shortcuts to the macOS version of KeePassium, even though it's an upscaled iPad version running on Apple Silicon, here are several approaches that could be considered:

1. Leverage AppKit via Catalyst Extensions

  • How: Since Catalyst doesn't offer full keyboard support on macOS by default, one option is to integrate AppKit APIs (native macOS frameworks) via the Mac Catalyst with AppKit extensions feature introduced by Apple. This allows for specific macOS-only behaviors (such as keyboard shortcuts and system-level UI navigation) to be handled using macOS native APIs while still maintaining Catalyst for cross-platform compatibility.
  • Potential implementation:

    • For keyboard navigation, use NSEvent to capture key press events and manipulate focus directly on macOS.
    • For handling keyboard shortcuts (like cmd+c or cmd+b), AppKit’s NSMenuItem and NSResponder could be utilized to hook into the macOS event loop and bind shortcut keys to specific actions like copying passwords or usernames.

    Why: This allows developers to keep the Catalyst base while only adding macOS-specific features as needed without compromising the app’s cross-platform nature.

2. Custom Keyboard Event Handling

  • How: Catalyst supports adding custom event handling through the UIKit UIResponder and UIKeyCommand APIs. Even though Catalyst doesn’t behave exactly like a native macOS app, you could create a custom handler for keyboard inputs when the app is running on macOS.

    • UIKeyCommand: Add custom keyboard shortcuts (like cmd+c for copying) using the addKeyCommand() method to assign commands.
    • UIResponder: Listen for keyDown and keyUp events and manually change the selection in the table view (or collection view) based on arrow key inputs.

    Why: This option is fully compatible with Catalyst, but it may require careful handling to ensure correct navigation behavior across different macOS versions.

3. Switch to Native macOS Controls When Catalyst is Running on macOS

  • How: Another approach is to conditionally switch to using native macOS controls (like NSTableView instead of UITableView) when the app is detected to be running on macOS. This can be done by leveraging Catalyst's capability to check the platform using #if targetEnvironment(macCatalyst) or by detecting the system at runtime.
  • Potential implementation:

    • When running on macOS, use macOS-specific API features to manage selection and shortcuts.
    • For navigation (up/down arrow keys), use the NSTableView's built-in selection behavior, which works seamlessly with macOS keyboard shortcuts.
    • For copying passwords (cmd+c), use NSTableViewDelegate's tableView(_:shouldSelectRow:) and menu(for:) methods to control interactions.

    Why: This option gives full access to macOS's native behavior while keeping Catalyst for cross-platform elements.

4. Handling Keyboard Focus via UIFocusGuide

  • How: You can also manage focus and keyboard navigation using UIKit’s UIFocusGuide. This is mainly intended for tvOS apps but can be adapted for macOS via Catalyst. It allows you to direct focus when the user presses certain keys like the arrow keys.

    • When the user presses the up or down arrows, you can explicitly set the focus to move to the next or previous element in the list of entries.

    Why: This approach keeps you within the Catalyst realm and avoids the need for macOS-specific APIs, but it still offers more control over keyboard navigation.

5. Use of Custom Gesture Recognizers for Keyboard Inputs

  • How: Create a custom gesture recognizer to handle keyboard events. This is typically used for detecting swipes or taps, but Catalyst allows gesture recognizers to be extended to handle keyboard input. For instance, you could set up a recognizer for the arrow keys (UIKeyCommand), and when those keys are pressed, the app would adjust selection focus to the appropriate entry.
  • Potential implementation:

    • Use gesture recognizers in combination with UIKeyCommand to override standard keyboard behavior and manually implement selection for entries when cmd+c or arrow keys are pressed.

    Why: This approach is still within Catalyst and offers more flexibility than relying purely on UIKit's default focus engine.

6. Create a macOS-specific Version of the App

  • How: If Catalyst's limitations become too much of a hurdle, you might consider creating a separate macOS-native version of the app. This would give you full access to AppKit and macOS's standard shortcut handling, but it would come at the cost of having to maintain two codebases (iOS and macOS versions separately).

    • This is a more drastic option but would resolve all platform-specific issues, as you could directly handle all keyboard events and macOS-native behaviors.

    Why: This offers the most control but introduces higher development and maintenance overhead.

7. macOS Catalyst Compatibility Checks

  • How: If the developer wants to retain Catalyst but work around macOS-specific issues, they can use UIDevice or ProcessInfo to detect the operating system and version, then apply platform-specific tweaks.

    • For macOS, they could use conditionals like if #available(macCatalyst 14.0, *) to apply specific behaviors only when the app runs on a Mac.
    • Custom handling for keyboard shortcuts and keyboard focus can be dynamically enabled or disabled based on the operating system version.

    Why: This allows the app to respond dynamically to the platform and version, ensuring proper keyboard handling.

Conclusion:

The developer has several potential paths to implement the keyboard shortcuts you’re requesting. Given that KeePassium uses Catalyst, the most straightforward solution might be a combination of UIKeyCommand for shortcut handling and AppKit extensions for enhanced keyboard support on macOS. This would enable more macOS-like behavior without sacrificing cross-platform compatibility. Alternatively, using UIResponder to capture key presses and manage navigation manually could offer a quick win with minimal disruption.

These suggestions should provide inspiration for how to tackle this issue while balancing macOS and Catalyst requirements.

Prompt:

On github I've opened an issue with the title "macOS: open entries using keyboard shortcuts"

I'm asking the KeePassium developer to add keyboard shortcuts for macOS version of KeePassium. However this is difficult because KeePassium uses Catalyst on macOS. Please suggest all options to add the keyboard shortcuts below to give the developer some inspiration where to start.

======================

My request:

Description After enabling a vault I hit cmd+f and there will appear some entries. When there are entries visible I want to open them with keyboard shortcuts. When hitting the down arrow I can select, but not open the entry. Why does KeePassium behave like this?

Expected behavior

cmd+f to start searching
↓ press the down arrow to move down in the list of entries (when cursor is active in the search field and you hit the down arrow, the first entry should be selected)
↑ press the up arrow to move up in the list of entries
cmd+c copies the password of the selected entry
cmd+b copies the username of the selected entry

======================

Developer responds:

Hi, thanks for the feedback and sorry for the delay.

Why does KeePassium behave like this?

This behavior depends on your macOS version and which KeePassium build you run.

The native Mac version of the app is built using Mac Catalyst. There, keyboard focus/selection depends on macOS version; the app does not handle this at all and I'm not sure if this is even possible without some dirty fragile low-level tricks.

To complicate things further, it is also possible to run the iOS version of KeePassium on an Apple Silicon Mac. In this case, the system pretends to be an iPad, and handles keyboard events — you guessed it — differently again. And since this is supposed to "just work", the app has little to no control over keyboard-based selection.

I have tested this case on macOS 13.2.1, and the right panel does automatically switch to the currently selected entry.

Can you confirm your macOS version, please?

======================

keepassium commented 3 days ago

I hope that you have the time

(Cries in sorted list of only public tasks)

Did you consider the following options?

I'm not sure how to respond here… This somehow moves the burden of debunking to me, with a disproportionally higher effort required.

Here's a brief response:

Which leaves us with "6. Create a macOS-specific version of the app", which is easier said than done :)

latvia234 commented 3 days ago

Sorry to bother you with these AI suggestions...

However it is interesting for me (and I guess for other non-developer users) to read your response. Interesting that suggestion 4 does not work as documented by Apple. Sounds like that there could be some possibilities here. Do you know for sure that everything has been done to try all possible things to work around this problem at suggestion 4? Does it help to contact Apple about this? Let's hope that one day Apple will give developers better tools to implement keyboard shortcuts for apps that are using Catalyst.

keepassium commented 1 day ago

Do you know for sure that everything has been done to try all possible things to work around this problem at suggestion 4?

No, I don't. Keyboard behavior is a black box with incorrect documentation. So first we have to learn the intended behavior, then analyze what happens instead, why this happens (us misunderstanding or docs misleading), learn whether we can change this, then apply ad-hoc fixes (if any found), and prey this won't break in the next macOS update. This is a full-blown research project, and there are other, higher-priority tasks in the queue.

Does it help to contact Apple about this?

Once we learn the real behavior, make sure it is indeed wrong documentation and not our mistake, then yes, it might help eventually. But getting to that stage requires more time than available for this issue at the moment.