2017-09-18 7 views
0

사용자가 Ctrl + S와 같은 조합을 입력 할 때 이벤트가 발생해야하는 텍스트 편집기 프로그램을 작성 중이므로 아래에서 수행 한 것으로 생각한 방식에 대한 코드를 제공했지만 이벤트 방아쇠를 당긴 것 같지 않습니다. 이벤트 처리기가 C#에서 초기화되는 방법에 대한 나의 경험 부족으로 인해 오류가 발생할 수 있습니다. 모든 도움을 주시면 감사하겠습니다.C#에서 키 입력을 잡는 방법

에서 Form1.cs :

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace Colied_Text 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void textBox1_CtrlS(object sender, KeyEventArgs e)  //Save file 
     { 
      if(e.Control && e.KeyCode == Keys.S) 
      { 
       textBox1.Text = "Ctrl+S for save"; 
      } 
     } 
    } 
} 

Form1.designer.cs입니다 :

namespace Colied_Text 
{ 
    partial class Form1 
    { 
     /// <summary> 
     /// Required designer variable. 
     /// </summary> 
     private System.ComponentModel.IContainer components = null; 

     /// <summary> 
     /// Clean up any resources being used. 
     /// </summary> 
     /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
     protected override void Dispose(bool disposing) 
     { 
      if (disposing && (components != null)) 
      { 
       components.Dispose(); 
      } 
      base.Dispose(disposing); 
     } 

     #region Windows Form Designer generated code 

     /// <summary> 
     /// Required method for Designer support - do not modify 
     /// the contents of this method with the code editor. 
     /// </summary> 
     private void InitializeComponent() 
     { 
      this.textBox1 = new System.Windows.Forms.TextBox(); 
      this.SuspendLayout(); 
      // 
      // textBox1 
      // 
      this.textBox1.AcceptsReturn = true; 
      this.textBox1.AcceptsTab = true; 
      this.textBox1.AllowDrop = true; 
      this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
      | System.Windows.Forms.AnchorStyles.Left) 
      | System.Windows.Forms.AnchorStyles.Right))); 
      this.textBox1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(192))))); 
      this.textBox1.Font = new System.Drawing.Font("Consolas", 10F, System.Drawing.FontStyle.Bold); 
      this.textBox1.ForeColor = System.Drawing.SystemColors.Menu; 
      this.textBox1.Location = new System.Drawing.Point(12, 10); 
      this.textBox1.MaxLength = 100000; 
      this.textBox1.Multiline = true; 
      this.textBox1.Name = "textBox1"; 
      this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both; 
      this.textBox1.Size = new System.Drawing.Size(645, 339); 
      this.textBox1.TabIndex = 0; 
      this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_CtrlS); 
      // 
      // Form1 
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
      this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(64))))); 
      this.ClientSize = new System.Drawing.Size(669, 361); 
      this.Controls.Add(this.textBox1); 
      this.Name = "Form1"; 
      this.Text = "CoTxEd"; 
      this.ResumeLayout(false); 
      this.PerformLayout(); 

     } 

     #endregion 

     private System.Windows.Forms.TextBox textBox1; 
    } 
} 
+0

@derloopkat 방금 입력 한 키 입력을 나타내는 name이라는 기능을 할당했습니다. –

답변

2

세트 속성 KeyPreviewtrue합니다. 포커스가있는 컨트롤에 이벤트가 전달되기 전에 양식에서 키 이벤트를받습니다.

public Form1() 
{ 
    InitializeComponent(); 
    this.KeyPreview = true; 
} 
+0

이것은 완벽하게 작동했습니다. 정말 고마워요! –