Click here to Skip to main content
15,867,141 members
Articles / Programming Languages / C#

MinLock, a KeePass 2.x Plugin to keep minimized KeePass Locked

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
14 Jun 2012CPOL2 min read 54.8K   2.2K   2   4
MinLock a KeePass 2.x plugin to keep it locked when minimized.

Introduction

KeePass is a "free, open source, light-weight and easy-to-use password manager."

MinLock is a simple plugin for KeePass 2.x that keeps a minimized KeePass locked (re-locks almost immediately after it becomes unlocked, so long as it is still minimized). 

This happens to be the case when KeePass is locked and minimized and then it's global auto-type feature is used, which can unlock KeePass and leave it unlocked.

To run, just download and extract the PLGX Plugin to your KeePass directory. 

Background  

KeePass has a short page about plugin development for version 2.x here

KeePass does have auto-lock features based on timers (e.g. idle time) that can achieve close to this but not quite as well.  

Using the code  

The small VS.NET solution is available (you'll have to fix the reference to KeePass.exe). 

Here's the full MinLock plugin class: 

C#
using System;
using System.Windows.Forms;
 
using KeePass.Plugins;
 
namespace MinLock
{
  public sealed class MinLockExt : Plugin
  {
    IPluginHost m_Host;
    Timer m_Timer;
 
    public override bool Initialize(IPluginHost host)
    {
      m_Host = host;
 
      // Upon unlocking, the database is opened and this event fires;
      // it likely fires other times too (e.g. opening db from menu).
      m_Host.MainWindow.FileOpened += MainWindow_FileOpened;
 
      return base.Initialize(host);
    }
 
    public override void Terminate()
    {
      KillTimer();
      base.Terminate();
    }
 
    void KillTimer()
    {
      if (m_Timer != null)
      {
        m_Timer.Dispose();
        m_Timer = null;
      }
    }
 
    void MainWindow_FileOpened(object sender, KeePass.Forms.FileOpenedEventArgs e)
    {
      if (m_Timer == null && m_Host.MainWindow.WindowState == FormWindowState.Minimized)
      {
        // Start a Windows.Forms.Timer, because it's based on the event loop it
        // can't interrupt current calls, though calls to Application.DoEvents
        // could wreak havoc, but that doesn't appear to be a problem here.
        m_Timer = new Timer();
        m_Timer.Interval = 1;
        m_Timer.Tick += Timer_Tick;
        m_Timer.Start();
      }
    }
 
    void Timer_Tick(object sender, EventArgs e)
    {
      KillTimer();
 
      if (m_Host.MainWindow.WindowState == FormWindowState.Minimized &&
          m_Host.MainWindow.IsAtLeastOneFileOpen())
      {
        m_Host.MainWindow.LockAllDocuments();
      }
    }
  }
}  

This plugin simply responds to a file being opened (which for KeePass, my understanding is that this is only when it has opened its secure database file).  Then KeePass has some things to finishing doing in the current callstack.  Instead of locking in the FileOpened event handler, I do a bit of hackery and use a Windows.Forms.Timer to wait just a bit before re-locking the KeePass workspace.  The timer can't fire until the underlying message pump runs, so in this way the plugin doesn't interrupt KeePass in the same callstack of the event firing.  KeePass continues on merrily, appears to do all it needs to do with the database, and then very shortly afterwards the message pump runs, the timer tick event fires, and MinLock re-locks KeePass Smile | <img src=    

MinLock was developed with KeePass version 2.19.  See the plugin development page for details about PLGX compatibility with future versions of KeePass. 

Points of Interest 

This approach turned out pretty well because MinLock re-locks KeePass before it even finishes auto-typing. 

KeePass plugins are cake.

This might be better off as an official KeePass option-based feature instead of a plugin.

MinLock does break one minor feature of KeePass.  If KeePass is minimized and locked, the user can right-click the tray icon and select "Unlock Workspace".  That feature of KeePass does not restore the KeePass Password Safe window, so almost instantly after unlocking the plugin will re-lock KeePass.  There are at least 3 other ways to unlock a minimized-and-locked KeePass that don't have this issue (because they all restore the KeePass window before presenting the unlock dialog); so use one of these instead: 1) double click the tray icon,  2) tray icon --> "Tray / Untray",  3) Crtl + Alt + K (default hotkey to show KeePass window). 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionNot Locking Pin
Danimal19692-Feb-23 4:15
Danimal19692-Feb-23 4:15 
QuestionMessage Closed Pin
19-Oct-21 2:57
Mary Rose Solero19-Oct-21 2:57 
QuestionGood article Pin
John681012-May-20 0:40
John681012-May-20 0:40 
QuestionGood article Pin
John681012-May-20 0:27
John681012-May-20 0:27 
QuestionPassword for zip? Pin
radhx17-Sep-12 2:01
radhx17-Sep-12 2:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.