Override VK_TTALK & VK_TEND

转自: http://kevinyisuihan.blog.163.com/blog/static/404793222008812942364/

When working on a project that involved creating a custom dialer for Windows Mobile devices, one of the more important tasks is enabling the detection of the Send (green, off-hook) and Hang-up (red, on-hook) button presses at all times. The difficulty with this feat is that the Windows Mobile OS has specific needs for these buttons so you have to be careful with how you go about using them.

For example the Send button is used for launching the phone application (cprog.exe by default), and also perform phone related tasks like making and answering calls or calling a contact. The Hang-up button is used to minimize dialogs and hang up active calls.

On some devices the Hang-up button can have other behaviors such as acting as a power button or enabling keyboard lock. It’s important to know these facts, because depending on what you intend to do with the Send or Hang-up buttons, you may cause existing features of the OS to not function.

There are several methods to detect the Send and Hang-up buttons, but only a few methods will let you safely co-exist with the existing features of the Windows Mobile OS. I’ll first talk about obtaining the key presses through use of keyboard hook (SetWindowsHook) and why you shouldn’t use it. This is the first method I used for the project but quickly learned about the various limitations. If you do an Internet search for “Windows Mobile SetWindowsHook” you will obtain several pages explaining how to use SetWindowsHook on the device to detect each key press. Using this method to detect the Send or Hang-up keys you may discover several set backs.

You are not just intercepting the Send or Hang-up keys but every key press so you’ll have to be extra careful about handling them.
If an existing application already has the keyboard hook, you may not be able to use it. On a device like the Palm Treo 700/750 you may find that the Key-guard feature no longer works. This is because the Palm Key-guard also relies on the keyboard hook and can no longer use it if another application has control of it.
You may find that certain features that rely on Send/Hang-up no long work. For example you can no longer dial a number from Contacts with the Send button.

There are ways around these limitations of course, but it would require a lot of manual handling from your application.

Another method of key detection is the use of hotkey registration. Microsoft provides us with the “RegisterHotKey” function which essentially allows you to register any button with your application window as a hotkey using the virtual key codes (VK_TTALK for Send, VK_TEND for Hang-up). What this does is, when the registered key is pressed; it will send you a WM_HOTKEY message, even if your window is not in the foreground, allowing you to handle it.

However there are a couple limitations when dealing with the Send and Hang-up keys. You may find that attempting to register either buttons as a hot key will fail. The reason is that the TaskBar (PPC) or Tray (Smartphone) already has Send button registered as a hotkey and the phone application has the Hang-up button.

You can forcibly “steal” the hot key using either the “UnregisterHotKey” function or the hidden “UnregisterFunc1” function; however taking the hot key away from their respective owners would render some features useless as with the cases when using “SetWindowsHook”.

After some experimentation I have found that there is a unique way in which you may register the Send and Hang-up buttons (and possibly other buttons) as hot keys. I’ve discovered that it is possible to register a button using 0x2001 as a modifier (some others will work as well such 0x2002, etc). What this does is it will generate a WM_HOTKEY message for you when pressing the Send button without interfering with the existing hot key registration held by the TaskBar or Tray or phone application. The limitation here, however, is that you will only get “one shot”; that is, only one WM_HOTKEY is generated and successive key presses will have no notifications. The solution is simply to register the key again. The code would look as follows (note that error and results checking are left out for simplicity)


// setup the window to receive the hot key message

ON_MESSAGE(WM_HOTKEY, &MyappDlg::OnHotkey)

...

// define some of the values to use for registering hot keys

#define ONE_SHOT_MOD 0x2001

#define VK_TTALK_ID 0x07

#define VK_TEND_ID 0x08

...

// register the Send and Hang-up buttons with the unique modifier

::RegisterHotKey(m_hWnd, VK_TTALK_ID, ONE_SHOT_MOD, VK_TTALK);

::RegisterHotKey(m_hWnd, VK_TEND_ID, ONE_SHOT_MOD, VK_TEND);

...

// wait for and handle the hot key messages

afx_msg LRESULT MyappDlg::OnHotkey( WPARAM wParam, LPARAM lParam )

{

switch (lParam >> 16)

{

case VK_TTALK:

// reregister VK_TTALK to get the next key press

::RegisterHotKey(m_hWnd, VK_TTALK_ID, ONE_SHOT_MOD, VK_TTALK);

// handle the keypress...

break;

case VK_TEND:

// reregister VK_TEND to get the next key press

::RegisterHotKey(m_hWnd, VK_TEND_ID, ONE_SHOT_MOD, VK_TEND);

// handle the keypress...

break;

...

So there you have it. Each time the Send and Hang-up buttons are pressed you get a notification and you don’t have to worry about interfering with the normal operations of each button.

本文来自优快云博客,转载请标明出处:http://blog.youkuaiyun.com/ice520301/archive/2009/12/31/5112448.aspx

### Java 中 KeyEvent.VK_CLEAR 键位的含义及用途 #### 1. `KeyEvent.VK_CLEAR` 的定义 在 Java 的 `java.awt.event.KeyEvent` 类中,`VK_CLEAR` 是一个静态常量,表示键盘上的“Clear”键。它的值为 12[^1]。此键通常用于清除当前选定的内容或重置输入字段。 #### 2. Clear 键的历史背景及用途 “Clear”键最初出现在某些旧式键盘上,尤其是在早期的计算机终端和专用设备中。它的功能类似于删除所有选定内容或清空输入框[^2]。然而,在现代键盘布局中,“Clear”键已不再常见,其功能可能被其他键(如 Delete 或 Backspace)取代。尽管如此,`KeyEvent.VK_CLEAR` 在 Java 中仍然保留,主要用于兼容性目的。 #### 3. 使用场景 以下是 `KeyEvent.VK_CLEAR` 的一些典型使用场景: - **模拟用户输入**:通过 `Robot` 类或其他自动化工具,可以模拟按下“Clear”键以清除文本框中的内容。 - **键盘事件处理**:在开发自定义组件时,可以通过捕获 `VK_CLEAR` 事件来实现特定的功能逻辑。例如,当用户按下“Clear”键时,程序可以自动清空某个输入框的内容。 #### 4. 示例代码 以下代码展示了如何捕获 `KeyEvent.VK_CLEAR` 并执行相应的操作: ```java import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JTextField; import javax.swing.JFrame; public class ClearKeyExample { public static void main(String[] args) { JFrame frame = new JFrame("Clear Key Example"); JTextField textField = new JTextField(20); textField.addKeyListener(new KeyListener() { @Override public void keyTyped(KeyEvent e) {} @Override public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_CLEAR) { textField.setText(""); // 清除文本框内容 } } @Override public void keyReleased(KeyEvent e) {} }); frame.add(textField); frame.setSize(300, 100); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } ``` #### 5. 注意事项 - **键盘布局差异**:由于现代键盘上很少有明确标记的“Clear”键,因此在实际应用中,`VK_CLEAR` 的触发可能依赖于特定的硬件或操作系统配置[^3]。 - **兼容性问题**:如果目标系统没有物理“Clear”键,则需要确保程序逻辑能够适配其他替代按键(如 Delete 或 Backspace)[^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值