2011-04-11 24 views
3

我的應用程序當前正在運行Windows Mobile 6.5的手持設備上運行。我希望能夠捕獲應用程序中的左/右軟鍵按鈕,並將用戶帶到「主頁」表單或應用程序內部經常使用的其他表單。在操作系統級別,這些軟鍵分別設置爲日曆/聯繫人,但是在我的應用程序中,我希望這些按鈕可以像上面提到的那樣操作。如何捕獲或攔截Compact Framework內的這些軟鍵按鈕?我已經做了一些研究,並看到了一些註冊熱鍵的參考資料?任何幫助將不勝感激。Capture左/右軟鍵按下 - C#.net CF 3.5

備註:我的應用程序使用MainMenu控件,但左/右軟鍵不控制任何菜單選項。

回答

2

試試這個:

public class HButtons : System.Windows.Forms.Form 
{ 
    private MainMenu mainMenu1; 
    private MenuItem mnuBack; 
    myMessageWindow messageWindow; 

    public HButtons() 
    { 
     InitializeComponent(); 

     this.messageWindow = new myMessageWindow(this); 
     RegisterHKeys.RegisterRecordKey(this.messageWindow.Hwnd); 
    } 
    protected override void Dispose(bool disposing) 
    { 
     RegisterHKeys.UnRegisterRecordKey(); 
     base.Dispose(disposing); 
    } 

    public void ButtonPressed(int button) 
    { 
     switch (button) 
      { 
       case (int)KeysHardware.VK_APP1: 
        MessageBox.Show("VK_APP1 pressed!"); 
        break; 
       case (int)KeysHardware.GreenPhoneButton: 
        MessageBox.Show("GreenPhoneButton pressed!"); 
        break; 
       case (int)KeysHardware.RedPhoneButton: 
        MessageBox.Show("RedPhoneButton pressed!"); 
        break; 

       case (int)KeysHardware.VK_TSOFT1: 
        MessageBox.Show("VK_TSOFT1 pressed!"); 
        break; 

      } 
     } 

     private void InitializeComponent() 
     { 
      this.mainMenu1 = new System.Windows.Forms.MainMenu(); 
      this.mnuBack = new System.Windows.Forms.MenuItem(); 
      this.SuspendLayout(); 
      // 
      // mainMenu1 
      // 
      this.mainMenu1.MenuItems.Add(this.mnuBack); 
      // 
      // mnuBack 
      // 
      this.mnuBack.Text = "Back"; 
      this.mnuBack.Click += new System.EventHandler(this.mnuBack_Click); 
      // 
      // HButtons 
      // 
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit; 
      this.ClientSize = new System.Drawing.Size(240, 268); 
      this.Menu = this.mainMenu1; 
      this.MinimizeBox = false; 
      this.Name = "HButtons"; 
      this.Text = "HW buttons"; 
      this.ResumeLayout(false); 

     } 

     private void mnuBack_Click(object sender, EventArgs e) 
     { 
      this.Close(); 
     } 
    } 

    public class myMessageWindow : MessageWindow 
    { 
     public const int WM_HOTKEY = 0x0312; 
     HButtons example; 
     public myMessageWindow(HButtons anExample) 
     { 
      this.example = anExample; 
     } 
     protected override void WndProc(ref Message msg) 
     { 
      switch (msg.Msg) 
      { 
       case WM_HOTKEY: 
        example.ButtonPressed(msg.WParam.ToInt32()); 
        return; 
      } 
      base.WndProc(ref msg); 
     } 
    } 

    public class RegisterHKeys 
    { 
     [DllImport("coredll.dll", SetLastError = true)] 
     public static extern bool RegisterHotKey(
      IntPtr hWnd, // handle to window 
      int id, // hot key identifier 
      KeyModifiers Modifiers, // key-modifier options 
      int key //virtual-key code 
     ); 

     [DllImport("coredll.dll")] 
     private static extern bool UnregisterFunc1(
      KeyModifiers modifiers, 
      int keyID); 

     public static void RegisterRecordKey(IntPtr hWnd) 
     { 
      UnregisterFunc1(KeyModifiers.Windows, (int)KeysHardware.VK_APP1); 
      RegisterHotKey(hWnd, (int)KeysHardware.VK_APP1, KeyModifiers.Windows, (int)KeysHardware.VK_APP1); 

      UnregisterFunc1(KeyModifiers.None, (int)KeysHardware.GreenPhoneButton); 
      RegisterHotKey(hWnd, (int)KeysHardware.GreenPhoneButton, KeyModifiers.None, (int)KeysHardware.GreenPhoneButton); 

      UnregisterFunc1(KeyModifiers.None, (int)KeysHardware.RedPhoneButton); 
      RegisterHotKey(hWnd, (int)KeysHardware.RedPhoneButton, KeyModifiers.None, (int)KeysHardware.RedPhoneButton); 

      UnregisterFunc1(KeyModifiers.None, (int)KeysHardware.VK_TSOFT1); 
      RegisterHotKey(hWnd, (int)KeysHardware.VK_TSOFT1, KeyModifiers.None, (int)KeysHardware.VK_TSOFT1); 

     } 

     public static void UnRegisterRecordKey() 
     { 
      UnregisterFunc1(KeyModifiers.Windows, (int)KeysHardware.VK_APP1); 
      UnregisterFunc1(KeyModifiers.None, (int)KeysHardware.GreenPhoneButton); 
      UnregisterFunc1(KeyModifiers.None, (int)KeysHardware.RedPhoneButton); 
      UnregisterFunc1(KeyModifiers.None, (int)KeysHardware.VK_TSOFT1); 
     } 
    } 

    ///// <summary> 
    ///// Summary description for hwButtons. 
    ///// </summary> 
    //public class hwButtons 
    //{ 
    // public hwButtons() 
    // { 
    //  HButtons theBtns = new HButtons(); 
    // } 
    //} 

    public enum KeyModifiers 
    { 
     None = 0, 
     Alt = 1, 
     Control = 2, 
     Shift = 4, 
     Windows = 8, 
     Modkeyup = 0x1000, 
    } 
    //public enum KeysHardware : int 
    //{ 
    // Hardware1 = 193, //0xC1 
    // Hardware2 = 194, 
    // Hardware3 = 195, 
    // Hardware4 = 196, 
    // Hardware5 = 197 
    //} 

    public enum KeysHardware : int 
    { 
     VK_F1 = 0x70, 
     VK_F2 = 0x71, 
     VK_F3 = 0x72, 
     VK_F4 = 0x73, 
     VK_F5 = 0x74, 
     VK_F6 = 0x75, 
     VK_F7 = 0x76, 
     VK_F8 = 0x77, 
     VK_F9 = 0x78, 
     VK_F10 = 0x79, 
     VK_F11 = 0x7A, 
     VK_F12 = 0x7B, 
     VK_TSOFT1 = VK_F1, // Softkey 1 
     VK_TSOFT2 = VK_F2, // Softkey 2 
     VK_TTALK = VK_F3, // Talk = Green Phone Button 
     VK_TEND = VK_F4, // End = Red Phone Button 
     VK_APP1 = 0xC1, // up to 6 other hardware buttons 
     VK_APP2 = 0xC2, 
     VK_APP3 = 0xC3, 
     VK_APP4 = 0xC4, 
     VK_APP5 = 0xC5, 
     VK_APP6 = 0xC6, 

     RedPhoneButton = VK_TEND, 
     GreenPhoneButton = VK_TTALK 
    } 
+0

我已經實施了您的解決方案。完美地工作在應用程序內部,但是一旦我退出應用程序,看起來好像UnRegisterRecordKey方法不會將軟鍵恢復到正常狀態(這意味着它們在操作系統級別返回時不會工作) – 2011-04-14 18:35:32

0

通常你只需要使用AllKeys(真)API CAL來捕獲所有按鍵。

但捕捉SoftKey1和SoftKey2上WM6.5的解決方案是更爲複雜:http://www.hjgode.de/wp/2012/09/20/windows-mobile-cf-how-to-catch-f1-and-f2-in-weh/

我的博客有節選:

  • 使用Microsoft.WindowsCE.Forms消息窗用的WndProc失敗。 [CF MessageWindow方法]
  • 如果僅在表單句柄上使用帶WndProc替換的SetWindowLong失敗。但是你可以使用它併爲表單中的每個子元素實現它。它看起來像消息沒有路由通過主要的WndProc(見下面的本地WINAPI),或者可以說,F1和F2鍵不會從子窗口到父窗口冒泡,如在Win32 API窗口應用程序中看到的。 [子類方法]
  • 使用OpenNetCF Application2/ApplicationEx和IMessageFilter工作。如果您已經使用或瞭解OpenNetCF/SmartDeviceFramework的其他需求,則可以使用此方法。 [SmartDeviceFramework Application2/IMessageFilter方法] 使用全局鍵盤掛鉤工作。這是我喜歡的,因爲它易於使用並提供所有鍵盤消息。 [KeybdHook方法]