如何在Timer或timer altnernative中多次按键? [英] How Multiple keypress in Timer or timer altnernative ?

查看:148
本文介绍了如何在Timer或timer altnernative中多次按键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void timer1_Tick(object sender, EventArgs e){ 
if (Form.ModifierKeys == System.Windows.Forms.Keys.Control && Form.ModifierKeys == System.Windows.Forms.Keys.Enter)

         my_translate(textbox1.text);
 
}





我试试但不工作怎么办?



我正在写一本字典软件;与计时器我检查确定按下的键,所以我翻译单词。我不能使用textBox1_KeyPress等因为我从.doc / .txt获取文本所以我需要定时器获取文本。







I try it but dont work how can I do it?

I am writing a dictionary software; with timer I check determine pressed keys so I translate word. I cant use textBox1_KeyPress etc. because I get text from .doc/.txt so I need timer get text.


//The code is working
private void timer1_Tick(object sender, EventArgs e){
 
    MouseButtons aa = MouseButtons;
    if (aa == MouseButtons.Middle && Form.ModifierKeys == Keys.Control)

            my_translate(textbox1.text);
}







我们有一个alnernative定时器在用户按下时调用方法一个关键的组合?




And We have a alnernative for timer to call a method when user pressed a key combination?

推荐答案

好的,我不同意你是怎么回事,但你正在尝试的是因为你试图检测按键应用于与您不同的应用程序。如果你想确定哪些键是关闭的,你可以使用这个类:



OK, I'm not agreeing with how you are going about this, but what you are trying is not working because you are trying to detect key presses in a different application than yours. If you want to determine what keys are down, you can use this class:

static class NativeMethods
{
    //See
    //http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx
    //For additional key codes

    private const int KEY_PRESSED = 0x8000;
    private const int VK_CONTROL = 0x11;
    private const int VK_RETURN = 0x0D;

    [DllImport("user32.dll")]
    static extern short GetKeyState(int key);

    public static bool IsControlEnterKeyDown()
    {
        return (GetKeyState(VK_CONTROL) & KEY_PRESSED) != 0 &&
               (GetKeyState(VK_RETURN) & KEY_PRESSED) != 0;
    }
}





在您的计时器代码中,您可以这样称呼它:





In your timer code you call it like this:

if (NativeMethods.IsControlEnterKeyDown())
{
    //Do your translation here
}





但是,请记住,当你的计时器打勾时你的钥匙不太可能会关闭,大多数好打字员打字每分钟60-80字,一个字是5字符,这意味着他们以5-6 PER SECOND的速度按键。键只能在几分之一秒内停机,可能大约10ms,这远远小于Timer可以达到的分辨率。



这意味着你的钥匙按下将是不可靠的,并且用户必须按住键直到你的计时器命中。



However, please keep in mind that its unlikely that your keys will be down when your timer ticks, most good typists type at 60-80 words per minute, a word being 5 characters, which means that they hit keys at the rate of 5-6 PER SECOND. The keys are only down for a fraction of a second, probably around 10ms, which is much less than the resolution of the Timer can hit.

This means that your key presses will be unreliable, and the user will have to hold the keys down until your timer hits.


这篇关于如何在Timer或timer altnernative中多次按键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆