Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
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);
 
}


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.


C#
//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);
}



And We have a alnernative for timer to call a method when user pressed a key combination?
Posted
Comments
[no name] 22-Jun-13 19:36pm    
When you ask the exact same question without providing any more relevant information as to what your problem is, you are likely to get the exact same answers. http://www.codeproject.com/Questions/610036/DetectplusMultiplepluskeypressplusinplustimerplusH
Member 9522119 22-Jun-13 19:43pm    
For More Details And Added Questions And Didnt solved
CHill60 22-Jun-13 20:08pm    
So your user could be in the middle of typing text into a text box, or your program could be in the middle of reading a text file, when your timer kicks in and suddenly starts to try to translate what's there so far? No wonder your problem is not solved. Make your mind up on the "trigger" for starting the action and that will guide you towards a suitable event. Hint - Timer is wrong

1 solution

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:

C#
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:

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


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.
 
Share this answer
 
Comments
Member 9522119 23-Jun-13 5:21am    
Thats work thank very much ;) and my timer interval is 100 for now it is not problem

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900