40 releases (10 stable)

new 2.1.0 May 18, 2025
1.3.1 Apr 19, 2025
1.2.1 Mar 3, 2025
1.0.1 Dec 15, 2024
0.2.1 Jul 28, 2024

#225 in GUI

Download history 55/week @ 2025-01-27 171/week @ 2025-02-03 57/week @ 2025-02-10 90/week @ 2025-02-17 321/week @ 2025-02-24 419/week @ 2025-03-03 200/week @ 2025-03-10 367/week @ 2025-03-17 227/week @ 2025-03-24 239/week @ 2025-03-31 196/week @ 2025-04-07 509/week @ 2025-04-14 519/week @ 2025-04-21 496/week @ 2025-04-28 550/week @ 2025-05-05 732/week @ 2025-05-12

2,405 downloads per month

MIT license

39KB
749 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 = "2.0"

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()
}

If you want to keep only CONTEXT_MENU, DEV_TOOLS, and RELOAD enabled in dev mode, you can build the plugin with tauri_plugin_prevent_default::debug instead.

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

Platform-specific options

Please read our versioning policy before using any of these options.

Windows

The unstable-windows feature must be enabled.

[dependencies]
tauri-plugin-prevent-default = { version = "2.0", features = ["unstable-windows"] }
use tauri_plugin_prevent_default::PlatformOptions;

tauri_plugin_prevent_default::Builder::new()
  .platform(PlatformOptions {
    // Whether general form information should be saved and autofilled.
    general_autofill: true,
    // Whether password information should be autosaved.
    password_autosave: false,
  })
  .build()

Versioning and Experimental Features

This plugin adheres to SemVer. However, features marked as unstable are experimental and may introduce breaking changes between minor versions.

Supported Tauri Version

This plugin requires Tauri 2.0 or later.

Dependencies

~19–71MB
~1M SLoC