Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

See if Shift is down while loading

Status
Not open for further replies.

tomcoombs

Programmer
Aug 14, 2003
65
GB
How can it be done?
I have tryed using an event (as below) But as its loading the event never fires. I need a function like :

bool IsAKeyPress (Keys.Shift) <<<<<<<<<<< MADE UP FUNCTION, anybody know a real one :) ?



Tryed : this.KeyDown += new KeyEventHandler(Form2_KeyDown);
///////////////////////////////////////////////////
if (!ShiftKeyPressed)
{ button1.PerformClick();
}
//////////////////////////////////////////////////
bool ShiftKeyPressed = false;
private void Form2_KeyDown(object sender, KeyEventArgs e)
{
ShiftKeyPressed = true;
}


All the best

Tom
 
Code:
this.KeyDown += new KeyEventHandler(Form2_KeyDown);
this.KeyUp += new KeyEventHandler(Form2_KeyDown);
bool ShiftKeyPressed = false;

private void Form2_KeyDown(object sender, KeyEventArgs e)
{
     ShiftKeyPressed = e.Shift;
}

private void Form2_KeyUp(object sender, KeyEventArgs e)
{
     ShiftKeyPressed = e.Shift;
}

but you also need to set
Code:
this.KeyPreview = true;
so that the keyboard events are passed to the form as well.
 
same story :-( ShiftKeyPressed never become true

Tom
 
put a breakpoint inside the keydown and keyup methods and see if the events are being triggere and if so, check the status of the shift key.

PS: you should set keypreview, keyup and keydown events in the constructor or better yet in the design mode.
 
Hello,

Create a label and try this.

private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyCode == Keys.ShiftKey)
{
label1.Text = "SHIFT KEY DOWN";
}
}

private void Form1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyCode == Keys.ShiftKey)
{
label1.Text = "SHIFT KEY RELEASED";
}
}
 
Hi,

Probably the easiest way to do this is to check Control.ModifierKeys while inside the form's Load event:

Code:
private void MyForm_Load(object sender, EventArgs e)
{
   Label newLabel = new Label();
   newLabel.Width = 150;
   if (Control.ModifierKeys == Keys.Shift)
   {
      newLabel.Text = "Shift key is down.";
   }	
   else
   {
      newLabel.Text = "Shift key is not down.";
   }
   this.Controls.Add(newLabel);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top