2010-06-22 2 views

Répondre

1

Je ne connais pas les détails dans C#, mais j'imagine quelque chose comme:

while (keyIsDown){ 
//do nothing 
} 

fonctionnerait. Je serais surpris s'il n'y a aucun moyen de garder une trace d'une touche enfoncée.

+0

Merci beaucoup les gars. Cela fonctionne parfaitement. – user367509

4

Vous pouvez utiliser la propriété SupressKeyPress sur KeyEventArgs:

public partial class Form1 : Form 
{ 
    private bool isKeyPressed = false; 
    public Form1() 
    { 
     InitializeComponent(); 
     this.textBox1.KeyDown += new KeyEventHandler(textBox1_KeyDown); 
     this.textBox1.KeyUp += new KeyEventHandler(textBox1_KeyUp); 
    } 

    void textBox1_KeyUp(object sender, KeyEventArgs e) 
    { 
     isKeyPressed = false; 
    } 

    void textBox1_KeyDown(object sender, KeyEventArgs e) 
    { 
     e.SuppressKeyPress = isKeyPressed; 
     isKeyPressed = true; 
    } 


} 
1
bool prevent = false; 
private void textBox1_KeyDown(object sender, KeyEventArgs e) 
{ 
    e.SuppressKeyPress = prevent; 
} 

private void textBox1_KeyUp(object sender, KeyEventArgs e) 
{ 
    prevent = false; 
} 

private void textBox1_TextChanged(object sender, EventArgs e) 
{ 
    prevent = true; 
} 
5

Juste une note supplémentaire pour tout le monde en utilisant les événements KeyDown et KeyUp pour supprimer la répétition des touches. Si vous faites cela vous devez exclure attraper des clés méta comme Alt/Shift etc sinon, lorsque ces touches sont enfoncées, la clé que vous voulez ne sera pas envoyée car il s'agit d'une combinaison de deux clés et les événements KeyDown et KeyUp capturent toutes les clés.

Cela s'applique à toutes les combinaisons à deux touches. Je n'ai pas ajouté toutes les clés méta à l'exemple suivant, seulement les plus courantes. Vous pouvez facilement ajouter vos propres clés en les ajoutant à la collection. Prolongation sur le poste de BFree:

public partial class Form1 : Form 
{ 
    private static readonly System.Collections.Generic.ICollection<System.Windows.Forms.Keys) ExcludeKeys = new System.Collections.Generic.HashSet<System.Windows.Forms.Keys)() 
    { 
     System.Windows.Forms.Keys.None, 
     System.Windows.Forms.Keys.Shift, 
     System.Windows.Forms.Keys.ShiftKey, 
     System.Windows.Forms.Keys.LShiftKey, 
     System.Windows.Forms.Keys.RShiftKey, 
     System.Windows.Forms.Keys.Alt, 
     System.Windows.Forms.Keys.Control, 
     System.Windows.Forms.Keys.ControlKey, 
     System.Windows.Forms.Keys.LControlKey, 
     System.Windows.Forms.Keys.RControlKey, 
     System.Windows.Forms.Keys.CapsLock, 
     System.Windows.Forms.Keys.NumLock, 
     System.Windows.Forms.Keys.LWin, 
     System.Windows.Forms.Keys.RWin 
    } 

    private bool isKeyPressed = false; 
    public Form1() 
    { 
     InitializeComponent(); 
     this.textBox1.KeyDown += new KeyEventHandler(textBox1_KeyDown); 
     this.textBox1.KeyUp += new KeyEventHandler(textBox1_KeyUp); 
    } 

    void textBox1_KeyUp(object sender, KeyEventArgs e) 
    { 
     if (!ExcludeKeys.Contains(e.KeyCode)) 
     { 
      isKeyPressed = false; 
     } 
    } 

    void textBox1_KeyDown(object sender, KeyEventArgs e) 
    { 
     if (!ExcludeKeys.Contains(e.KeyCode)) 
     { 
      e.SuppressKeyPress = isKeyPressed; 
      isKeyPressed = true; 
     } 
    } 
} 
Questions connexes