65.9K
CodeProject 正在变化。 阅读更多。
Home

MinLock,一个KeePass 2.x插件,用于保持最小化的KeePass锁定

starIconstarIconstarIconstarIconstarIcon

5.00/5 (2投票s)

2012年6月14日

CPOL

2分钟阅读

viewsIcon

58690

downloadIcon

2296

MinLock是一个KeePass 2.x插件,用于在最小化时保持其锁定状态。

引言

KeePass 是一个“免费、开源、轻量级且易于使用的密码管理器。

MinLock 是 KeePass 2.x 的一个简单插件,可以保持一个最小化的 KeePass 锁定状态(在解锁后几乎立即重新锁定,只要它仍然最小化)。 

当 KeePass 锁定并最小化,然后使用其全局自动输入功能时,就会发生这种情况,这可能会解锁 KeePass 并使其保持解锁状态。

要运行,只需下载并提取 PLGX 插件到您的 KeePass 目录即可。 

背景  

KeePass 有一个关于版本 2.x 插件开发的简短页面 在这里。 

KeePass 确实具有基于计时器的自动锁定功能(例如空闲时间),可以实现与此类似的效果,但效果不如此好。  

使用代码

提供了小的 VS.NET 解决方案(您需要修复对 KeePass.exe 的引用)。 

这是完整的 MinLock 插件类: 

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

此插件只是响应文件被打开(对于 KeePass 而言,我的理解是这仅在它打开了其安全数据库文件时发生)。 然后 KeePass 在当前调用堆栈中还有一些事情要做。 我没有在 FileOpened 事件处理程序中锁定,而是进行了一些技巧,并使用 Windows.Forms.Timer 等待片刻后再重新锁定 KeePass 工作区。 计时器无法在底层消息泵运行之前触发,因此通过这种方式,插件不会在事件触发的相同调用堆栈中中断 KeePass。 KeePass 继续愉快地运行,似乎完成了所有需要与数据库相关的事情,然后过了一会儿,消息泵运行,计时器滴答事件触发,MinLock 重新锁定 KeePass Smile | <img src=    

MinLock 是使用 KeePass 版本 2.19 开发的。 有关 PLGX 与 KeePass 未来版本的兼容性的详细信息,请参阅插件开发 页面。 

兴趣点 

这种方法效果很好,因为 MinLock 在 KeePass 甚至完成自动输入之前就重新锁定它。

KeePass 插件就是这么简单。

这可能更适合作为 KeePass 的官方基于选项的功能,而不是插件。

MinLock 会破坏 KeePass 的一个次要功能。 如果 KeePass 最小化并锁定,用户可以右键单击托盘图标并选择“解锁工作区”。 KeePass 的此功能不会恢复 KeePass 密码保险箱窗口,因此在解锁后几乎立即插件将重新锁定 KeePass。 至少有 3 种其他方法可以解锁最小化和锁定的 KeePass,而这些方法没有这个问题(因为它们都在呈现解锁对话框之前恢复 KeePass 窗口);因此请使用以下方法之一:1) 双击托盘图标, 2) 托盘图标 --> “托盘/取消托盘”, 3) Ctrl + Alt + K(默认快捷键以显示 KeePass 窗口)。 

© . All rights reserved.