⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hotkeymanager.html

📁 热键管理控件源代码
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>HotKeyManager, ver. 1.7.0</title>

<style type='text/css'>
/*  body         { font-family: MS Sans Serif; font-size: 12pt; } */
  h1           { font: bold 14pt Arial; margin-bottom: 10px; }
  h2           { font: bold 12pt Arial; margin-bottom: 10px; margin-top: 40px; }
  td.name      { background-color: #FF9999; width: 120px; font-weight: bold; }
  td.desc      { background-color: #F0F0F0; }
  td.default   { background-color: #F0F0F0; }
  ul           { margin-left: 15px; margin-top: 5px; }
  li           { margin-bottom: 10px; }
  .declaration { font-weight: bold; }
</style>

</head>


<body topmargin='15' bottommargin='15' leftmargin='10' rightmargin='10'>

<a name='Top'><h1>HotKeyManager, ver. 1.7.0</h1>


<a href='#Properties'>[Properties]</a> <a href='#Methods'>[Methods]</a> <a href='#Events'>[Events]</a>
<a href='#StaticMethods'>[Static Methods]</a>
<br>
<a href='#Hints'>[Hints]</a> <a href='#Bugs'>[Known Bugs]</a> <a href='#Comments'>[Comments]</a>
<br>


<p>
HotKeyManager is a component that allows you to add system-wide hotkeys to your application.
</p>

<p>
A hotkey in Windows is a key combination which invokes a specific action.
You can often use Ctrl+S from an application to save a file. However, a <i>system-wide</i> 
hotkey allows your app. to invoke a custom action from anywhere. Your app. doesn't have 
to be in the foreground or even visible.
</p>

<p>
In Windows 2000 and later it is even possible to use the extended keys found on modern keyboards 
(those that start your internet browser, your e-mail program, adjust your sound volume, etc).
</p>


<a name='Properties'></a><h2>Properties</h2>

No properties.



<a name='Methods'></a><h2>Methods</h2>

<table cellpadding='2' cellspacing='2' border='0' width='100%'>
<tr>
  <td class='name' valign='top'>AddHotKey</td>
  <td class='desc' valign='top'><span class='declaration'>function AddHotKey(HotKey: Cardinal): Word;</span><br>
  Registers a hotkey with a specific key combination.<br>
  <b>NOTE:</b> A return value of 0 means the operation failed - perhaps because the specified 
  key combination is already in use.</td>
  <td class='default' valign='top'>Returns an index or 0 if error</td>
</tr>
<tr>
  <td class='name' valign='top'>ChangeHotKey</td>
  <td class='desc' valign='top'><span class='declaration'>function ChangeHotKey(Index: Word; NewHotKey: Cardinal): Word;</span><br>
  Changes a hotkey to a new key combination. Provide the index received from AddHotKey as 
  parameter.<br>
  <b>NOTE:</b> This method returns a new index. The old index is no longer valid.</td>
  <td class='default' valign='top'>Returns an index or 0 if error</td>
</tr>
<tr>
  <td class='name' valign='top'>RemoveHotKey</td>
  <td class='desc' valign='top'><span class='declaration'>function RemoveHotKey(HotKey: Cardinal): Boolean;</span><br>
  Unregisters a hotkey by its key combination.</td>
  <td class='default' valign='top'>Returns true or false</td>
</tr>
<tr>
  <td class='name' valign='top'>RemoveHotKeyByIndex</td>
  <td class='desc' valign='top'><span class='declaration'>function RemoveHotKeyByIndex(Index: Word): Boolean;</span><br>
  Unregisters a hotkey by the index previously received from AddHotKey or ChangeHotKey.</td>
  <td class='default' valign='top'>Returns true or false</td>
</tr>
<tr>
  <td class='name' valign='top'>ClearHotKeys</td>
  <td class='desc' valign='top'><span class='declaration'>procedure ClearHotKeys;</span><br>
  Unregisters all registered hotkeys.</td>
  <td class='default' valign='top'><br></td>
</tr>
</table>



<a name='Events'></a><h2>Events</h2>

<table cellpadding='2' cellspacing='2' border='0' width='100%'>
<tr>
  <td class='name' valign='top'>OnHotKeyPressed</td>
  <td class='desc' valign='top'><span class='declaration'>type TOnHotKeyPressed = procedure(HotKey: Cardinal; Index: Word) of object;</span><br>
  Fired when then user presses one of the registered hotkeys.
  The event method provides the key combination that was pressed and the index the hotkey
  is registered under (the index is previously received from AddHotKey or ChangeHotKey).</td>
</tr>
</table>



<a name='StaticMethods'></a><h2>Static Methods</h2>

<table cellpadding='2' cellspacing='2' border='0' width='100%'>
<tr>
  <td class='name' valign='top'>HotKeyAvailable</td>
  <td class='desc' valign='top'><span class='declaration'>function HotKeyAvailable(HotKey: Cardinal): Boolean;</span><br>
  Tests if the specified key combination is available for registration by your app.
  (or any other app.).</td>
  <td class='default' valign='top'>Returns true or false</td>
</tr>
<!--
<tr>
  <td class='name' valign='top'>HotKeyValid</td>
  <td class='desc' valign='top'><span class='declaration'>function HotKeyValid(HotKey: Cardinal): Boolean;</span><br>
  Tests if the specified key combination is valid. It works the same as the HotKeyAvailable
  method, except it will ignore if your app. already registered the hotkey.<br>
  HotKeyAvailable is useful before you atempt to assign a hotkey, while HotKeyValid is
  useful after you registered the hotkey.</td>
  <td class='default' valign='top'>Returns true or false</td>
</tr>
-->
<tr>
  <td class='name' valign='top'>GetHotKey</td>
  <td class='desc' valign='top'><span class='declaration'>function GetHotKey(Modifiers, Key: Word): Cardinal;</span><br>
  Returns the key combination resulting from the specified modifiers (a combination of 
  Shift, Ctrl, Alt, Win, or none of them) and the key (A, for instance).<br>
  <b>USAGE:</b> The modifiers can be a combination of MOD_SHIFT, MOD_ALT, MOD_CONTROL, and MOD_WIN.
  The key is a virtual keycode. For alphanumeric chars you can get their
  virtual keycode by using the Ord method on the capitalized char - eg. Ord('G')
  corresponds to the virtual keycode for the letter 'G'. For any other chars,
  you should use the appropriate ASCII code or VK_xxx constant (found in windows.pas).
  Alternatively, use the TextToHotKey method.<br>
  <b>EXAMPLE 1:</b> <code>hk := GetHotKey(MOD_CONTROL + MOD_SHIFT, Ord('G'));</code><br>
  <b>EXAMPLE 2:</b> <code>hk := GetHotKey(MOD_ALT + MOD_SHIFT, VK_HOME);</code></td>
  <td class='default' valign='top'>Returns 0 if error</td>
</tr>
<tr>
  <td class='name' valign='top'>SeparateHotKey</td>
  <td class='desc' valign='top'><span class='declaration'>procedure SeparateHotKey(HotKey: Cardinal; var Modifiers, Key: Word);</span><br>
  Splits a key combination into a key (A, for instance) and its modifiers (shift, ctrl, alt, win)
  and returns them.</td>
  <td class='default' valign='top'><br></td>
</tr>
<tr>
  <td class='name' valign='top'>HotKeyToText</td>
  <td class='desc' valign='top'><span class='declaration'>function HotKeyToText(HotKey: Cardinal; Localized: Boolean): String;</span><br>
  Returns the specified key combination in a string.<br>
  <b>USAGE:</b> If Localized is true the result is a string in the user's currently active 
  language, otherwise the result is in US English. Unless you have a good reason you should 
  always present key combinations in the local language (Localized=True), as this is less 
  confusing for the user (who otherwise may have difficulties identifying the correct keys).
  If the active language is US English there is no difference between the localized and 
  the non-localized result.<br>
  <b>NOTE:</b> An extended key may not have a name in Windows. If not, a name defined internally 
  in HotKeyManager is used. This name is always in US English, regardless of the value of 
  Localized.</td>
  <td class='default' valign='top'>Returns an empty string if error</td>
</tr>
<tr>
  <td class='name' valign='top'>TextToHotKey</td>
  <td class='desc' valign='top'><span class='declaration'>function TextToHotKey(Text: String; Localized: Boolean): Cardinal;</span><br>
  Returns a key combination from the specified string.<br>
  <b>USAGE:</b> If Localized is true it means Text is in the user's currently active language,
  otherwise Text is in US English.<br>
  You must use '+' as separator.<br>
  You can use extra spaces if you like.<br>
  The method is case insensitive.<br>
  The order of the modifiers doesn't matter.<br>
  <b>NOTE:</b> An extended key may not have a name in Windows. If not, a name defined internally 
  in HotKeyManager is used. This name is always in US English, regardless of the value of 
  Localized.<br>
  <b>EXAMPLE 1:</b> <code>hk := TextToHotKey('Ctrl+Alt+Num 3', True);</code><br>
  <b>EXAMPLE 2:</b> <code>hk := TextToHotKey('alt + ctrl + num 3', True);  // The same</code></td>
  <td class='default' valign='top'>Returns 0 if error</td>
</tr>
<tr>
  <td class='name' valign='top'>IsExtendedKey</td>
  <td class='desc' valign='top'><span class='declaration'>function IsExtendedKey(Key: Word): Boolean;</span><br>
  Returns whether the specified key is an extended key (used for internet browser, e-mail, etc).<br>
  <b>USAGE:</b> Be aware that Key is not a hotkey. It is the key you get after a call to 
  SeparateHotKey (ie. a hotkey without the modifiers).</td>
  <td class='default' valign='top'>Returns true or false</td>
</tr>
</table>



<a name='Hints'></a><h2>Hints</h2>

<ul>
<li><b>What is a good key combination?</b><br>
Avoid simple key combinations that are likely to be used by applications. For instance, if you
define the hotkey 'Ctrl+S' it means that applications that normally use Ctrl+S will NOT
be notified the user pressed that key combination.
</li>

<li><b>How do I test if a key combination contains the Ctrl modifier (like in Ctrl+Alt+X)?</b><br>
You can use SeparateHotKey, then test the returned Modifier variable, like so:<pre>
  SeparateHotKey(HotKey, Modifiers, Key);        // Split HotKey into Modifiers and Key
  CtrlFound  := (Modifiers and MOD_CONTROL) <> 0;
  AltFound   := (Modifiers and MOD_ALT) <> 0;
  ShiftFound := (Modifiers and MOD_SHIFT) <> 0;
  WinFound   := (Modifiers and MOD_WIN) <> 0;    // Special Windows modifier key</pre>
</li>

<li><b>Can I use the Win modifier key (found on Windows-certified keyboards)?</b><br>
Yes, although the modifier should be considered reserved for Windows itself.
Be aware that the THotKey control which comes with Delphi does not support the
Win modifier key. Use either the GetHotKey or the TextToHotKey method (see
examples above).
</li>

<li><b>Can I use HotKeyManager in a DLL?</b><br>
Yes, the component registers the hotkeys using indexes that are system-wide unique,
ensuring no conflicts with other system-wide hotkeys.
</li>

<li><b>Is it possible to find out which handle a hotkey belongs to?</b><br>
No, Windows does not expose this information. So you can't know which application 
registered a specific hotkey.
</li>

<li><b>Tell me more about hotkeys?</b><br>
See API documentation on RegisterHotKey, UnregisterHotKey, and WM_HOTKEY.
</li>
</ul>



<a name='Bugs'></a><h2>Known Bugs</h2>

<ul>
<li>It's not really a bug, just the way Windows works: Some key combinations
can be registered, but can't be invoked. You should avoid Num Lock, Caps Lock,
Scroll Lock, Pause, and Print Screen - especially in combination with the
Ctrl modifier. Also don't use F12 as it's reserved for debugging.
</li>

<li>In addition to the extended keys defined by the Windows 2000/XP API there exist
a number of other extended keys, that do things like print, start the Calculator 
application, or forward a mail message. HotKeyManager will not be able to use these 
extended keys.
</li>

<li>Even if you use only the approved extended keys you can't be certain they will work
in a key combination. Some keyboards do not follow Microsoft's standard. Also, the keys 
may not be activated because a keyboard driver is not installed.
</li>
</ul>



<a name='Comments'></a><h2>Comments</h2>

HotKeyManager is <i>free for personal and commercial use</i>. Feel free to use and improve it,
but <i>please include all original files if you redistribute the zip-file</i>.
If you have any comments or corrections I would very much like to hear them.
</p>

<p>
Get the latest version from <a href='http://subsimple.com/delphi.asp'>http://subsimple.com/delphi.asp</a>.
</p>

<p>
Troels Jakobsen<br>
<a href='mailto:delphihacker@get2net.dk'>delphihacker@get2net.dk</a>
</p>

</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -