473,320 Members | 1,922 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

C# .Net Keyboard problem

Hello
=====
I am trying to do a simple exercise using Visual Studio C#.
I have a form with one picturebox and some buttons.
I want to be able to control a picture box graphic using the keyboard
(especially arrow keys). All works well by using the Form1_KeyDown()
method .. until I add buttons to the form .. then the keyboard control
ceases to work.
I have looked for properties to change to give the form priority but
can't see anything.
Advice would be much appreciated.
TIA
===
Brian
===
Sep 8 '07 #1
7 4938
My advice is that you move your project to WPF. WPF doesn't have these
issues you're seeing because of their RoutedEvent model. It's much
easier, faster in panning the picture, and better all around.

Sep 8 '07 #2
Not much to go on here, but Setting KeyPreview to true may help (if not
already set to true).

http://msdn2.microsoft.com/en-us/lib...eypreview.aspx

-HTH
"Brian Ward" <br********@zetnet.co.ukwrote in message
news:31************************@zetnet.co.uk...
Hello
=====
I am trying to do a simple exercise using Visual Studio C#.
I have a form with one picturebox and some buttons.
I want to be able to control a picture box graphic using the keyboard
(especially arrow keys). All works well by using the Form1_KeyDown()
method .. until I add buttons to the form .. then the keyboard control
ceases to work.
I have looked for properties to change to give the form priority but
can't see anything.
Advice would be much appreciated.
TIA
===
Brian
===

Sep 8 '07 #3
Brian Ward wrote:
Hello
=====
I am trying to do a simple exercise using Visual Studio C#.
I have a form with one picturebox and some buttons.
I want to be able to control a picture box graphic using the keyboard
(especially arrow keys). All works well by using the Form1_KeyDown()
method .. until I add buttons to the form .. then the keyboard control
ceases to work.
Probably because the Button controls have the focus when you are trying
use the keyboard, and so are getting the key events.
I have looked for properties to change to give the form priority but
can't see anything.
Sounds like you want the Form.KeyPreview property, set to "true". This
will cause the Form instance to receive all of the key events before
they are sent to the control that actually has focus. Set the "Handled"
property in the appropriate event args class if you actually handle the
key event, so that you don't get parallel handling of the key (ie the
form reacts to a key and some other control does at the same time).

I don't believe that your issue has anything to do with using WPF or
not. I can't imagine that given your specific question, rewriting your
project to use WPF just so that the form can get key events is an
appropriate solution.

Pete
Sep 8 '07 #4
Thanks Peter (and Smithers) .. setting KeyPreview to true is a partial
solution.
This now seems to enable me to detect Keys such as U, D etc. but
strangely not the arrow keys.
I am using this code in the Form1_KeyDown() method:

string input = e.KeyData.ToString();

if (input == "Down" || input == "D")
{
y = y + 10;
} // etc.

Without the buttons, this used to work with both the "Down" arrow and
the "D" key .. now only the "D" works (when KeyPreview is set .. better
than nothing though!)
==
Brian
==
Sep 9 '07 #5
Brian Ward wrote:
Thanks Peter (and Smithers) .. setting KeyPreview to true is a partial
solution.
This now seems to enable me to detect Keys such as U, D etc. but
strangely not the arrow keys.
Ah. Sorry...I should have paid closer attention in your original post
that you want to include arrow keys.

For those, they are handled as "dialog keys", unless the control getting
the key events returns true from the IsInputKey() method. I haven't
tried this myself, so you'll have to experiment a little. It's possible
that you can override IsInputKey() in your form, returning "true" for
the arrow keys.

But it's possible that even setting KeyPreview to "true" won't cause the
form's IsInputKey() method to be called (like I said, haven't tried it
myself). If so, you should be able to override ProcessDialogKey and
handle the arrow keys there.

Pete
Sep 9 '07 #6
Brian Ward wrote:
string input = e.KeyData.ToString();

if (input == "Down" || input == "D")
{
y = y + 10;
} // etc.
Oh, and in addition to my other post...

I would strongly recommend just using the Keys constants, rather than
calling ToString() and then doing string comparisons. Doing so would be
much more efficient, in terms of code size, data size, and run-time
performance.

Pete
Sep 9 '07 #7
Thanks Pete
=========
Overriding ProcessCmdKey() seems to do the job!
Returning true when arrow keys, etc. detected, false otherwise.
Thanks for your help.
==
Brian
==
Sep 10 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: hokiegal99 | last post by:
I am a relative c newbie, so bare with me. I need to write a program that writes all keyboard keystrokes into a text file (up to 500KB at which point it would begin overwriting itself)...
7
by: Don Riesbeck Jr. | last post by:
I'm working on an application (OEM) using C# that utilizes input from a keyboard, and USB Barcode Scanner. The scanner is a HID Keyboard device, and input from it is sent to the system as if it...
0
by: rs | last post by:
Hi guys, I am trying to read from a USB keyboard using vb.net and HID classes. the USB keyboard is not my primary keyboard. I have a ps2 keyboard connected and is detected in device manager as...
2
by: rs | last post by:
Hi guys, I am trying to read from a USB keyboard using vb.net and HID classes. the USB keyboard is not my primary keyboard. I have a ps2 keyboard connected and is detected in device manager as...
3
by: nhmark64 | last post by:
Hi, Sometimes when I am cutting and pasting it looks like the keyboard layout changes; for instance ',' and 'w' are interchanged. Is there a better way to fix this problem than exiting then...
7
by: jpierson | last post by:
Hi, I am tryin to create a keyboard hook that sends the keystroke ctrl + pause/break. I haven't used keyboard hooks before so I'm not too sure how to use them public int MyKeyboardProc(int...
0
by: Nuno | last post by:
Hello, I'm new on C# and i'm trying to implement a control that traps a certain sequence of keys to use for shortcut key functionality like the one used in the the...
1
by: cvairetti | last post by:
Hi, I just write an application in C# that show an interface with five button, each button starts an external application that it comes in front of the main C# application with focus on. When the...
2
by: Justin | last post by:
So I have a keyboard hook that I have implemeted into my c# app. I need to not allow any hotkey actions to be performed when my app is opened. I can capture the key events and handle them if I want...
1
by: Aycex | last post by:
I am taking my first java class as i am a Database person this is quite interesting and here is my problem. We are supposed to be learning to overload a class and i believe i have my code correct...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.