Click here to Skip to main content
Click here to Skip to main content
Go to top

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

, 14 Jun 2012
Rate this:
Please Sign up or sign in to vote.
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: 

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)

Share

About the Author

TLHobbes

United States United States
No Biography provided

Comments and Discussions

 
QuestionPassword for zip? [modified] Pinmemberradhx17-Sep-12 2:01 
Hi,
 
Downloaded zip file asks for a password. What is the password?
(tried downloading many times... other downloads are ok)
 
If there is no password then most probably the zip file is corrupt.
 
Please check.
 
Regards.
 
** Update: **
 
Opened the download link in another tab (using Google Chrome) and download worked.
(Previously download link was directly clicked)
Using Free Download Manager.
 
Regards.

modified 17-Sep-12 8:10am.

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140921.1 | Last Updated 14 Jun 2012
Article Copyright 2012 by TLHobbes
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid