• Upgrade your Internet Experience
  • Sign in
  • Microsoft.com
  • United States (English)
    Brasil (Português)Česká republika (Čeština)Deutschland (Deutsch)España (Español)France (Français)Italia (Italiano)Россия (Русский)대한민국 (한국어)中华人民共和国 (中文)台灣 (中文)日本 (日本語)香港特别行政區 (中文)
 
 
.NET Framework Developer Center
 
 
Home
 
 
Library
 
 
Learn
 
 
Downloads
 
 
Support
 
 
Community
 
 
Forums
 
 
 
.NET Framework Developer Center > .NET Development Forums > Windows Presentation Foundation (WPF) > Canvas KeyDown event
Ask a questionAsk a question
Search Forums:
  • Search Windows Presentation Foundation (WPF) Forum Search Windows Presentation Foundation (WPF) Forum
  • Search All .NET Development Forums Search All .NET Development Forums
  • Search All MSDN Forums Search All MSDN Forums
 

AnswerCanvas KeyDown event

  • Friday, July 07, 2006 3:57 PMA.Kahn Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0

    I can't able to get the keyword events of the canvas....

    Any help would be appreciated.

    Thanks in advance

    Cheers,

    G

    • ReplyReply
    • QuoteQuote
     

Answers

  • Friday, July 07, 2006 5:49 PMviliescuModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Vote As Helpful
    0
    You can do that by setting Focusable="True" on the canvas and set the focus on the Canvas from the start (myCanvas.Focus() somewhere in the window's constructor or loaded event).
    The problem is that if another element receives focus during the application's lifetime, you will not receive the keyboard events on the canvas.

    Another way is to set the event handler up in the hierarchy (in the parent Window) - because of the tunneling/bubbling you will get the event no matter where the focus is in the window. I think in this case PreviewKeyDown is even better - you will get the event before any child will process it.
    • ReplyReply
    • QuoteQuote
     

All Replies

  • Friday, July 07, 2006 4:33 PMKeith Boyd -MSFT Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0
    You need to have an element that can accept keyboard input within your
    Canvas for the KeyDown event to occur. I'm able to get the KeyDown event
    to fire by doing the following:


    private void CreateAndShowMainWindow()

    {

    Canvas myCanvas = new Canvas();

    myCanvas.KeyDown += new
    System.Windows.Input.KeyEventHandler(myCanvas_KeyDown);

    TextBox myTextBox = new TextBox();

    myTextBox.Width = 100;

    Canvas.SetTop(myTextBox, 300);

    Canvas.SetLeft(myTextBox, 25);

    myCanvas.Children.Add(myTextBox);

    }

    void myCanvas_KeyDown(object sender, System.Windows.Input.KeyEventArgs
    e)

    {

    MessageBox.Show("keydown event occurred");

    }

    If you just have UIElements that can't receive user input (like a
    TextBlock or other UIElement) and you try typing anywhere in the Canvas
    the event doesn't get raised.

    Good luck.
    -Keith

    • ReplyReply
    • QuoteQuote
     
  • Monday, July 10, 2006 11:19 AMA.Kahn Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0
    thanks guys :) your comments really help me to solved the problem.
    Cheers
    • ReplyReply
    • QuoteQuote
     
Need Help with Forums? (FAQ)
 
© 2009 Microsoft Corporation. All rights reserved.
Terms of Use
|
Trademarks
|
Privacy Statement