#tauri-plugin #tauri #plugin

sys tauri-plugin-prevent-default

Disable default browser shortcuts

32 releases (2 stable)

1.0.1 Dec 15, 2024
0.8.0 Dec 9, 2024
0.7.7 Nov 26, 2024
0.2.1 Jul 28, 2024

#464 in GUI

Download history 40/week @ 2024-09-21 274/week @ 2024-09-28 92/week @ 2024-10-05 527/week @ 2024-10-12 22/week @ 2024-10-19 190/week @ 2024-10-26 76/week @ 2024-11-02 26/week @ 2024-11-09 22/week @ 2024-11-16 259/week @ 2024-11-23 192/week @ 2024-11-30 293/week @ 2024-12-07 261/week @ 2024-12-14 52/week @ 2024-12-21 29/week @ 2024-12-28 76/week @ 2025-01-04

507 downloads per month

MIT license

38KB
833 lines

tauri-plugin-prevent-default

Disable default browser shortcuts in your Tauri app, e.g. F3 or Ctrl+J.

Install

Install the plugin by adding the following to your Cargo.toml file:

[dependencies]
tauri-plugin-prevent-default = 1.0

If using custom listeners, you must also enable the required permissions:

src-tauri/capabilities/prevent-default.json

{
  "identifier": "prevent-default",
  "windows": ["*"],
  "permissions": ["prevent-default:default"]
}

Usage

Register the plugin with Tauri:

src-tauri/src/main.rs

tauri::Builder::default()
  .plugin(tauri_plugin_prevent_default::init())
  .run(tauri::generate_context!())
  .expect("error while running tauri application");

You can also use flags to determine which shortcuts the plugin should disable. By default, it will disable all of them.

use tauri_plugin_prevent_default::Flags;

let prevent = tauri_plugin_prevent_default::Builder::new()
  .with_flags(Flags::CONTEXT_MENU | Flags::PRINT | Flags::DOWNLOADS)
  .build();

tauri::Builder::default()
  .plugin(prevent)
  .run(tauri::generate_context!())
  .expect("error while running tauri application");
  • Disable all but a few:
use tauri_plugin_prevent_default::Flags;

// This will disable all shortcuts, except `FIND` and `RELOAD`.
tauri_plugin_prevent_default::Builder::new()
  .with_flags(Flags::all().difference(Flags::FIND | Flags::RELOAD))
  .build()
  • Disable only keyboard shortcuts:
use tauri_plugin_prevent_default::Flags;

tauri_plugin_prevent_default::Builder::new()
  .with_flags(Flags::keyboard())
  .build()
  • Disable custom shortcuts:
use tauri_plugin_prevent_default::KeyboardShortcut;
use tauri_plugin_prevent_default::ModifierKey::{CtrlKey, ShiftKey};

tauri_plugin_prevent_default::Builder::new()
  .shortcut(KeyboardShortcut::new("F12"))
  .shortcut(KeyboardShortcut::with_modifiers("E", &[CtrlKey, ShiftKey]))
  .shortcut(KeyboardShortcut::with_shift_alt("I"))
  .build();
  • Keep certain shortcuts enabled only when in dev mode:
fn main() {
  tauri::Builder::default()
    .plugin(prevent_default())
    .run(tauri::generate_context!())
    .expect("error while running tauri application");
}

#[cfg(debug_assertions)]
fn prevent_default() -> tauri::plugin::TauriPlugin<tauri::Wry> {
  use tauri_plugin_prevent_default::Flags;

  tauri_plugin_prevent_default::Builder::new()
    .with_flags(Flags::all().difference(Flags::DEV_TOOLS | Flags::RELOAD))
    .build()
}

#[cfg(not(debug_assertions))]
fn prevent_default() -> tauri::plugin::TauriPlugin<tauri::Wry> {
  tauri_plugin_prevent_default::init()
}

Platform-specific options

Windows

The unstable-native-windows feature must be enabled.

[dependencies]
tauri-plugin-prevent-default = { version = "1.0", features = ["unstable-native-windows"] }
tauri_plugin_prevent_default::Builder::new()
  // Whether general form information should be saved and autofilled.
  // Defaults to `true`.
  .general_autofill(false)
  // Whether password information should be autosaved.
  // Defaults to `false`.
  .password_autosave(false)
  .build()

Experimental features

Cargo features prefixed with unstable- are experimental and may introduce breaking changes between patch versions or even be completely removed.

Note

The plugin should work fine on Windows, but there are still tests to be done on MacOS and Linux. If you encounter any problems on these platforms, please open an issue.

Supported Tauri Version

This plugin requires Tauri 2.0 or later.

Dependencies

~18–69MB
~1M SLoC