Click here to Skip to main content
15,886,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I need to detect if a key (A-Z 0-9) on the keyboard has been pressed nothing fancy in C# for Visual Studio 2008 I think it's some like
C#
public delegate void KeyPressEventHandler(object? sender, KeyPressEventArgs e);
to set up the handler

All the examples are for later versions of C#/VS that I can see using a part of
C#
System Windows.Forms
that either does not exist in 2008
or I am not using right? Any Ideas anyone?

What I have tried:

C#
public partial class Form1 : Form
{
     bool KeyBoard = false;

Later in the code where the detection is needed
C#
label1.Text = KeyBoard.ToString();
        //for(int i = 0; i <= 100;i++)
          while(!(KeyBoard == true))
          {


This is sitting inside the click routine for a button...

If I use a for loop it works for a set period. If I attach a KeyDown Event to the form with
C#
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
    KeyBoard = true;
}
Posted
Updated 5-Jan-21 23:59pm
Comments
Richard MacCutchan 6-Jan-21 6:41am    
I just noticed your comment to Luc. I do not think that capturing keystrokes will do anything useful for you. If you want to prevent the screensaver kicking in then you should disable it. The whole point of a screensaver is that its timer gets reset every time the mouse is physically moved, or a key is physically pressed. You could try periodically printing something on the screen, but I am not sure if that will do anything.
glennPattonWork3 6-Jan-21 7:46am    
The thing is the user account I am trying to use for this test doesn't have rights to turn the screen saver off, printing a character was one of the things I have also tried an animated JPEG of a butterfly. The saver seems to respond to mouse movements only (?)...
Richard MacCutchan 6-Jan-21 8:12am    
Sorry, I don't know of any other way to disable it.

Hi,

using System.Windows.Forms;

takes two periods; it has been present since day one of .NET, and is still there.

Key handling hasn't changed over time, it works the same on all versions of C# and Visual Studio. Hence, all documentation and all correct examples should allow you to solve whatever it is you are trying (your question is a bit mysterious about it).

That said, I don't like
while(!(KeyBoard == true))

as that suggests a busy wait loop, something that is a big nono on Windows; event driven code should be used consistently.

:)
 
Share this answer
 
Comments
glennPattonWork3 6-Jan-21 4:07am    
What it is I'm trying to move the mouse pointer to prevent a screen saver taking over. Not I'm not trying to create code to make it look like I'm working, I am trying to keep the screen alive to measure pixel brightness...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900