2017-03-18 1 views
0
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 

namespace clipper 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
     } 

     public void HandleKeyDownEvent(object sender, KeyEventArgs e) 
     { 
      MessageBox.Show("It came here"); 

      if (e.Key == Key.LeftCtrl && e.Key == Key.C) 
      { 
       MessageBox.Show("You have pressed control + c"); 
      } 
     } 
    } 
} 

이것은 내 코드입니다. 나는 keyPress 이벤트에 대한 솔루션 검색을 시도하고 여기 그것을 구현했습니다. 그러나 C#을 처음 접했을 때 나는 무엇이 잘못되었는지 확신하지 못했습니다. 나를 안내 해줘.나는 C# wpf에서 키 스트로크를 얻으려고하는데 작동하지 않는다. 코드가 잘못 되었나요?

답변

1

와 KEY_UP 사용해야합니다. 도움이 될 수 있습니다 ..

private void Form1_Load(object sender, EventArgs e) 
{ 
    KeyPreview = true; 
} 

private void Form1_KeyDown(object sender, KeyEventArgs e) 
{ 
    if (e.Modifiers == (Keys.Shift | Keys.Control)) 
    { 
     MessageBox.Show("."); 
    } 
} 
+0

나는 Form ...을 사용하지 않고있다. 나는 wpf를 사용하고있다. 아마도 내가 KeyPreview = ture; – laslavinco

+0

이뿐 아니라 이벤트를 첨부하는 것을 잊었습니다 :'this.KeyPreview = true; this.KeyPress + = 새 KeyPressEventHandler (HandleKeyDownEvent); – SilentStorm

1

당신은 당신이 trueKeyPreview을 변경해야하고 e.Modifiersevent을 시도 할 수 있습니다 다음 코드

private void textBox_KeyUp(object sender, KeyEventArgs e) 
{ 
    if ((e.Key == Key.C) && Keyboard.IsKeyDown(Key.LeftCtrl)) 
    { 
     MessageBox.Show("You have pressed control + c"); 
    } 
} 
+0

작동하지 않습니다. 그것은 여전히 ​​방법을 부르지 않는다. 이 이벤트를 트리거하려면 UI에 어떤 객체가 있어야합니까? – laslavinco

+0

Sajeetharan

+0

owh .. TexBox가 ​​아닌 다른 것을 사용할 수 있습니까? 버튼 같은 걸? textBox_KeyUp를 button_KeyUp로 변경 하시겠습니까? – laslavinco

관련 문제