Compartilhar via


How to: Override Smartphone Soft Keys 

Normally, Smartphone soft keys operate menus; however, you can provide custom soft key functionality by removing the MainMenu component from your form. When there is no menu in a Smartphone application, Soft Key 1 and Soft Key 2 presses raise the KeyDown event followed by a KeyUp event when the keys are released.

The KeyCode field recognizes F1 as Soft Key 1, and F1 as Soft Key 2.

Example

The following code example shows how to provide event handling code for soft key presses.

Visual C# users need to define an event hander for the KeyPress event in the form's constructor.

// Connect an event handler to the KeyPress event
this.KeyPress += new KeyPressEventHandler(OnKeyPress);
Private Sub keypressed(ByVal o As [Object], _
    ByVal e As KeyPressEventArgs) Handles MyBase.KeyPress
     ' Determine if ESC key value is raised.
     If e.KeyChar = ChrW(27) Then
         ' Handle the event to provide your own functionality.
         e.Handled = True

         ' Add  your event handling code here.
         MsgBox("Custom back key functionality.")  
     End If
  End Sub
private void OnKeyPress(object sender, KeyPressEventArgs ke)
{
  // Determine if ESC key value is raised.
  if (ke.KeyChar == (Char)Keys.Escape)
  {
      // Handle the event to provide functionality.
      ke.Handled = true;

      // Add your event handling code here.
     MessageBox.Show("Custom back key functionality.");
  }
}

Compiling the Code

This example requires references to the following namespaces:

See Also

Tasks

How to: Override the Smartphone Back Key

Other Resources

Smartphone Development and the .NET Compact Framework