Welcome,
 Guest User.

  Click to Login   


 FarPoint HOME
 clubFarPoint HOME

SUPPORT
   Forum
   View Your Questions

RESOURCES
   Search
   Articles
   Product Docs
   FAQ
   Training Videos
   Feature Suggestions
   Subscriptions
   User Groups
   FarPoint Blog

DOWNLOAD
   Updates
   Trials

MY ACCOUNT
   Member Benefits
   MVP Status
   RSS Feeds
   Register Product
   View Products
   View Orders
   Order Now
   My Profile
   Product Profiles

   clubFarPoint Feedback...


There are 16 registered members online.


   

Articles
<< Return to Articles
Handling User Interaction with the Keyboard
Submitted By: Bill Albing, FarPoint Technologies Last Updated: 6/18/2007 1:34:21 PM
Level: Intermediate  
Product(s): Spread for Windows Forms 2, Spread for Windows Forms 2.5 for VS2002/2003, Spread for Windows Forms 2.5 for VS2005  
Language(s): C#, Visual Basic.NET  
Description: There are many ways to handle user input from the keyboard. Here are just a few tips.  
 

To view a printable version of this article in a PDF viewer, click here for PDF.


Overview

FarPoint Spread defines default operations of particular keys (input maps) and defines certain user actions (action maps) both of which you can customize.

For example, pressing the Tab key moves the focus to the next cell by default; changing the input map definition for this Tab key enables you move the focus to a cell in a different direction such as down.

Here are three examples of how the user interaction with the keyboard can be handled. Since the first step is to detect any keyboard activity, the first two examples deal with that.

The third deals with changing the active cell based on keyboard input.

  • Detecting Keyboard Activity in Cells Not Being Edited
  • Detecting Keyboard Activity in Cells Being Edited
  • Changing the Active Cell with a Custom Action Class

Detecting Keyboard Entry in Cells Not Being Edited

You can detect respective key entries on sheets by using KeyDown/KeyPress/KeyUp events in FpSpread class (inherit from System.Windows.Forms.Control, respectively).

C# Code

private void fpSpread1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
     Console.WriteLine("KeyDown event: " + e.KeyCode.ToString());
 }

private void fpSpread1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
     Console.WriteLine("KeyPress event: " + e.KeyChar.ToString());
 }

private void fpSpread1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
     Console.WriteLine("KeyUp event: " + e.KeyCode.ToString());
 }

Visual Basic .NET Code

Private Sub FpSpread1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles FpSpread1.KeyDown
     Console.WriteLine("KeyDown event: " + e.KeyCode.ToString)
End Sub

Private Sub FpSpread1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles FpSpread1.KeyPress
     Console.WriteLine("KeyPress event: " + e.KeyChar.ToString)
End Sub

Private Sub FpSpread1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles FpSpread1.KeyUp
     Console.WriteLine("KeyUp event: " + e.KeyCode.ToString)
End Sub

Detecting Keyboard Entry in Cells Being Edited

Basically, respective Key events (KeyDown/KeyPress/KeyUp events in FpSpread class which inherit System.Windows.Forms.Control, respectively) do not occur in cells being edited.

These Key events can be fired by having editor controls (FpSpread1.EditingControl) to be used while editing to handle them.

These Key events occurrence conditions largely depend on input map definitions. Please make double check in advance when you want to implement them.

C# Code

private void fpSpread1_EditModeOn(object sender, System.EventArgs e)
{

     //Handle respective Key events at the timing when cell editing begins.
     fpSpread1.EditingControl.KeyDown += new KeyEventHandler(this.fpSpread1_KeyDown);
    
     fpSpread1.EditingControl.KeyPress += new KeyPressEventHandler(this.fpSpread1_KeyPress);
    
     fpSpread1.EditingControl.KeyUp += new KeyEventHandler(this.fpSpread1_KeyUp);
    
}

private void fpSpread1_EditModeOff(object sender, System.EventArgs e)
{

     //Release respective Key event handles at the timing when cell editing is complete.
     fpSpread1.EditingControl.KeyDown -= new KeyEventHandler(this.fpSpread1_KeyDown);

     fpSpread1.EditingControl.KeyPress -= new KeyPressEventHandler(this.fpSpread1_KeyPress);

     fpSpread1.EditingControl.KeyUp -= new KeyEventHandler(this.fpSpread1_KeyUp);

}

private void fpSpread1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
     Console.WriteLine("KeyDown event: " + e.KeyCode.ToString());
}

private void fpSpread1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
     Console.WriteLine("KeyPress event: " + e.KeyChar.ToString());
}

private void fpSpread1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
     Console.WriteLine("KeyUp event: " + e.KeyCode.ToString());
}

Visual Basic .NET Code

Private Sub FpSpread1_EditModeOn(ByVal sender As Object, ByVal e As System.EventArgs) Handles FpSpread1.EditModeOn

     'Handle respective Key events at the timing when cell editing begins.
     Dim KeyDownHandler As KeyEventHandler = AddressOf FpSpread1_KeyDown
     AddHandler FpSpread1.EditingControl.KeyDown, KeyDownHandler

     Dim KeyPressHandler As KeyPressEventHandler = AddressOf FpSpread1_KeyPress
     AddHandler FpSpread1.EditingControl.KeyPress, KeyPressHandler

     Dim KeyUpHandler As KeyEventHandler = AddressOf FpSpread1_KeyUp
     AddHandler FpSpread1.EditingControl.KeyUp, KeyUpHandler

End Sub

Private Sub FpSpread1_EditModeOff(ByVal sender As Object, ByVal e As System.EventArgs) Handles FpSpread1.EditModeOff

     'Release respective Key event handles at the timing when cell editing is complete.
     Dim KeyDownHandler As KeyEventHandler = AddressOf FpSpread1_KeyDown
     RemoveHandler FpSpread1.EditingControl.KeyDown, KeyDownHandler

     Dim KeyPressHandler As KeyPressEventHandler = AddressOf FpSpread1_KeyPress
     RemoveHandler FpSpread1.EditingControl.KeyPress, KeyPressHandler

     Dim KeyUpHandler As KeyEventHandler = AddressOf FpSpread1_KeyUp
     RemoveHandler FpSpread1.EditingControl.KeyUp, KeyUpHandler

End Sub

Private Sub FpSpread1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles FpSpread1.KeyDown
     Console.WriteLine("KeyDown event: " + e.KeyCode.ToString)
End Sub

Private Sub FpSpread1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles FpSpread1.KeyPress
     Console.WriteLine("KeyPress event: " + e.KeyChar.ToString)
End Sub

Private Sub FpSpread1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles FpSpread1.KeyUp
     Console.WriteLine("KeyUp event: " + e.KeyCode.ToString)
End Sub

Changing the Active Cell with a Custom Action Class

Here is an example of how to create a custom action map by creating subclasses that inherit members from existing classes.You can create your own actions by creating subclasses that inherit the Action class other than input map fields that have been provided from SpreadActions class. In this code, you can see how to change active cells background color by pressing F8 key.

C# Code

private void Form1_Load(object sender, System.EventArgs e)
{
    
     FarPoint.Win.Spread.InputMap im = new FarPoint.Win.Spread.InputMap();
     im = fpSpread1.GetInputMap(FarPoint.Win.Spread.InputMapMode.WhenFocused)

   

Subscribe to the RSS feed!RSS Subscribe


Click for Morrisville, North Carolina Forecast
 
Todays Trivia














FarPoint's Spread for Web Forms

     About Us | Contact UsComments & Suggestions | ©2008 FarPoint Technologies, Inc.