Home / WORDPRESS / How to Disable Hotkeys such as F12, CTRL+U, CTRL+Shift+C on both your Blogger & WordPress

How to Disable Hotkeys such as F12, CTRL+U, CTRL+Shift+C on both your Blogger & WordPress

Are you looking to prevent users from accessing hotkeys or shortcut keys like CTRL+C, CTRL+U, and CTRL+Shift+C on your Blogger or WordPress website? This guide is tailored for you.

Disabling hotkeys on your website can enhance security by reducing the risk of unauthorized access to your source code and deterring content copying. While it’s not feasible to completely block determined users from viewing your code, disabling common hotkeys can thwart casual attempts.

In this guide, we will demonstrate how to achieve this using JavaScript code, applicable to both Blogger and WordPress websites.

Let’s get started.

Overview of Hotkeys and Their Functions

The Reason You Need To Disable Hotkeys

Hotkeys like F12 and Ctrl+Shift+C grant access to Developer Tools, while Ctrl+U enables users to view the page source. By disabling these hotkeys, you can:

  • Mitigate casual content theft.

  • Restrict easy access to your website’s code.

  • Safeguard proprietary logic or design elements.

Steps to Disable Hotkeys on Your Website

You can implement the following code snippet on your Blogger, WordPress, or other CMS platforms to block key combinations like F12, Ctrl+U, Ctrl+Shift+I, and more.

<style>
  #custom-notification {
      display: none;
      position: fixed;
      top: 20px;
      right: 20px;
      background-color: #f44336;
      color: white;
      padding: 15px;
      border-radius: 5px;
      z-index: 1000;
      font-family: Arial, sans-serif;
      font-size: 14px;
      box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
      animation: slide-in 0.3s ease, slide-out 0.3s ease 2.7s forwards;
  }

  @keyframes slide-in {
      from {
          opacity: 0;
          transform: translateX(100%);
      }
      to {
          opacity: 1;
          transform: translateX(0);
      }
  }

  @keyframes slide-out {
      from {
          opacity: 1;
          transform: translateX(0);
      }
      to {
          opacity: 0;
          transform: translateX(100%);
      }
  }
</style>

<div id="custom-notification" style="display:none; position:fixed; top:20px; right:20px; background-color:#f44336; color:white; padding:15px; border-radius:5px; z-index:1000; box-shadow: 0 4px 6px rgba(0,0,0,0.1);">
  Hotkey disabled.
</div>

<script>
  document.addEventListener('keydown', function (event) {
      // Block F12
      if (event.key === 'F12') {
          event.preventDefault();
          showCustomNotification('F12 is disabled on this site.');
      }

      // Block Ctrl+U (View Source)
      if (event.ctrlKey && event.key === 'u') {
          event.preventDefault();
          showCustomNotification('Ctrl+U is disabled on this site.');
      }

      // Block Ctrl+Shift+I (DevTools)
      if (event.ctrlKey && event.shiftKey && event.key === 'I') {
          event.preventDefault();
          showCustomNotification('Ctrl+Shift+I is disabled on this site.');
      }
      if (event.ctrlKey && event.key === 'p') {
          event.preventDefault();
          showCustomNotification('Printing is disabled on this site.');
      }

      // Block Ctrl+C (Copy)
      if (event.ctrlKey && event.key === 'c') {
          event.preventDefault();
          showCustomNotification('Copying content is disabled on this site.');
      }

      // Block Ctrl+Shift+C (Inspect Element)
      if (event.ctrlKey && event.shiftKey && event.key === 'C') {
          event.preventDefault();
          showCustomNotification('Ctrl+Shift+C is disabled on this site.');
      }
  });

  document.addEventListener('contextmenu', function (event) {
      event.preventDefault();
      showCustomNotification('Right-click is disabled on this site.');
  });

  function showCustomNotification(message) {
      const notification = document.getElementById('custom-notification');
      notification.textContent = message;
      notification.style.display = 'block';

      // Hide the notification after 3 seconds
      setTimeout(() => {
          notification.style.display = 'none';
      }, 3000);
  }
</script>

Important Points to Note

  1. Not Fully Secure: Savvy users can bypass these restrictions by disabling JavaScript or using external tools. These actions serve as a deterrent rather than an absolute solution.

  2. Consider User Experience: Disabling hotkeys might inconvenience legitimate users. Employ this method only when necessary.

  3. Emphasize Server-Side Security: For safeguarding sensitive data, prioritize backend security measures over frontend solutions.

Additional Resources:

In Conclusion

By deactivating hotkeys such as F12, Ctrl+U, and Ctrl+Shift+C on your website, you can discourage casual users from accessing your source code or duplicating content.

By following the outlined steps, you can enhance your website’s security. Remember, these measures are not foolproof, and reinforcing server-side security is vital for protecting sensitive information.

Feel free to test this code on your website and share this guide with individuals who may benefit from this information.

Leave a Reply

Your email address will not be published. Required fields are marked *

ad