Click here to Skip to main content
15,887,428 members
Home / Discussions / C#
   

C#

 
GeneralRe: Using WFP application for webcam. I do not want to show the image control on window. Using a function from pages to capture the image on certain occassion and save it. Pin
Paul Conrad7-Sep-15 6:19
professionalPaul Conrad7-Sep-15 6:19 
QuestionHow can i get complex matrix svd(singlar value decompostion) lib? Pin
smallkubi5-Sep-15 17:45
smallkubi5-Sep-15 17:45 
QuestionSubreport in SSRS is getting error when calling throuch C# windows application Pin
hishamkmr4-Sep-15 22:19
hishamkmr4-Sep-15 22:19 
QuestionUse cutter for cut an image(.svg) with a definition layout image by winform (C#) Pin
phinq19104-Sep-15 16:32
professionalphinq19104-Sep-15 16:32 
AnswerRe: Use cutter for cut an image(.svg) with a definition layout image by winform (C#) Pin
OriginalGriff4-Sep-15 21:48
mveOriginalGriff4-Sep-15 21:48 
GeneralRe: Use cutter for cut an image(.svg) with a definition layout image by winform (C#) Pin
phinq19105-Sep-15 7:41
professionalphinq19105-Sep-15 7:41 
GeneralRe: Use cutter for cut an image(.svg) with a definition layout image by winform (C#) Pin
Dave Kreskowiak5-Sep-15 10:52
mveDave Kreskowiak5-Sep-15 10:52 
Questioncant send key stroke only to Word Through my program that use KeyboardHook Pin
goldsoft4-Sep-15 4:30
goldsoft4-Sep-15 4:30 
hi,

i have my program that Captures weighing and send it to a open window.

its work excellent , except with a single software - Microsoft Word

I have tried everything and have no idea why.

my code:

this is the GlobalKeyBoardHook.cs class
public class GlobalKeyboardHook
{
    [DllImport("user32.dll")]
    static extern int CallNextHookEx(IntPtr hhk, int code, int wParam, ref keyBoardHookStruct lParam);
    [DllImport("user32.dll")]
    static extern IntPtr SetWindowsHookEx(int idHook, LLKeyboardHook callback, IntPtr hInstance, uint theardID);
    [DllImport("user32.dll")]
    static extern bool UnhookWindowsHookEx(IntPtr hInstance);
    [DllImport("kernel32.dll")]
    static extern IntPtr LoadLibrary(string lpFileName);

    public delegate int LLKeyboardHook(int Code, int wParam, ref keyBoardHookStruct lParam);

    public struct keyBoardHookStruct
    {
        public int vkCode;
        public int scanCode;
        public int flags;
        public int time;
        public int dwExtraInfo;
    }

    const int WH_KEYBOARD_LL = 13;
    const int WM_KEYDOWN = 0x0100;
    const int WM_KEYUP = 0x0101;
    const int WM_SYSKEYDOWN = 0x0104;
    const int WM_SYSKEYUP = 0x0105;

    LLKeyboardHook llkh;
    public List<Keys> HookedKeys = new List<Keys>();

    IntPtr Hook = IntPtr.Zero;

    public event KeyEventHandler KeyDown;
    public event KeyEventHandler KeyUp;

    // This is the Constructor. This is the code that runs every time you create a new GlobalKeyboardHook object
    public GlobalKeyboardHook()
    {
        llkh = new LLKeyboardHook(HookProc);
        // This starts the hook. You can leave this as comment and you have to start it manually (the thing I do in the tutorial, with the button)
        // Or delete the comment mark and your hook will start automatically when your program starts (because a new GlobalKeyboardHook object is created)
        // That's why there are duplicates, because you start it twice! I'm sorry, I haven't noticed this...
        // hook(); <-- Choose!
    }
    ~GlobalKeyboardHook()
    { unhook(); }

    public void hook()
    {
        IntPtr hInstance = LoadLibrary("User32");
        Hook = SetWindowsHookEx(WH_KEYBOARD_LL, llkh, hInstance, 0);
    }

    public void unhook()
    {
        UnhookWindowsHookEx(Hook);
    }

    public int HookProc(int Code, int wParam, ref keyBoardHookStruct lParam)
    {
        if (Code >= 0)
        {
            Keys key = (Keys)lParam.vkCode;
            if (HookedKeys.Contains(key))
            {
                KeyEventArgs kArg = new KeyEventArgs(key);
                if ((wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN) && (KeyDown != null))
                    KeyDown(this, kArg);
                else if ((wParam == WM_KEYUP || wParam == WM_SYSKEYUP) && (KeyUp != null))
                    KeyUp(this, kArg);
                if (kArg.Handled)
                    return 1;
            }
        }
        return CallNextHookEx(Hook, Code, wParam, ref lParam);
    }

// --- from here start the program --
GlobalKeyboardHook gHook;
gHook = new GlobalKeyboardHook(); // Create a new GlobalKeyboardHook
gHook.KeyDown += new KeyEventHandler(gHook_KeyDown);
foreach (Keys key in Enum.GetValues(typeof(Keys)))
{
     gHook.HookedKeys.Add(key);
}
gHook.hook();
 public void gHook_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == KeyPressg)  //Pause-Break //19
            {
                ProccName = (GetActiveWindowTitle().ToString());
                Start(ProccName);
                gHook.unhook();
                gHook.hook();
            }
            else
            {

            }
        }
  private string GetActiveWindowTitle()
        {
            const int nChars = 256;
            StringBuilder Buff = new StringBuilder(nChars);
            IntPtr handle = GetForegroundWindow();
            if (GetWindowText(handle, Buff, nChars) > 0)
            {
                return Buff.ToString();
            }

            return null;
        }

        [DllImport("user32.dll")]
        static extern IntPtr GetForegroundWindow();

        [DllImport("user32.dll", CharSet = CharSet.Auto)]<br />
        static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);

        [DllImport("user32.dll")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);

        public void Start(string NAME)
        {

             MSG = lblMSG.Text.Trim();
            IntPtr zero = IntPtr.Zero;
            for (int i = 0; (i < 60) && (zero == IntPtr.Zero); i++) 
            {
                Thread.Sleep(500);
                zero = FindWindow(null, NAME);
            }
            if (zero != IntPtr.Zero)
            {
                try
                {
                    string text = lblMSG.Text.Trim();
                    text = text.Replace("gHook", "");

                    //OLD
                    //foreach (char c in text)
                    //    SendKeys.SendWait(c.ToString());

                    //NEW
                    if (text.Length == 0) return;
                    SendKeys.SendWait(text);

                    if (MyParam._KeyAfter == "Enter")
                    {
                        MyParam.FromKEY = true;
                        SendKeys.SendWait("{ENTER}");
                    }
                    else if (MyParam._KeyAfter == "TAB")
                    {
                        SendKeys.SendWait("{TAB}");
                    }
                    else if (MyParam._KeyAfter == "Key Down")
                    {
                        SendKeys.SendWait("{DOWN}");
                    }

                    SendKeys.Flush();

                }
                catch { }
            }
        }

Any ideas why ?

thanks

EDIT

C#
public void Start(string NAME)
        {
            // --> NAME  Contain -->    Word‪ - 123.docx  for example when Word window open

<pre>
        MSG = lblMSG.Text.Trim();
        IntPtr zero = IntPtr.Zero;
        for (int i = 0; (i < 60) && (zero == IntPtr.Zero); i++) 
        {
            Thread.Sleep(500);
            zero = FindWindow(null, NAME);  // <-- in Word zero Always Contain 0  And never goes on in the code so stuck
        }
        // <--- the program stops here and stuck 
        if (zero != IntPtr.Zero)
        {</pre>


modified 5-Sep-15 6:09am.

AnswerRe: cant send key stroke only to Word Through my program that use KeyboardHook Pin
Eddy Vluggen4-Sep-15 4:45
professionalEddy Vluggen4-Sep-15 4:45 
GeneralRe: cant send key stroke only to Word Through my program that use KeyboardHook Pin
goldsoft5-Sep-15 0:10
goldsoft5-Sep-15 0:10 
AnswerRe: cant send key stroke only to Word Through my program that use KeyboardHook Pin
Dave Kreskowiak4-Sep-15 5:21
mveDave Kreskowiak4-Sep-15 5:21 
GeneralRe: cant send key stroke only to Word Through my program that use KeyboardHook Pin
goldsoft4-Sep-15 6:16
goldsoft4-Sep-15 6:16 
GeneralRe: cant send key stroke only to Word Through my program that use KeyboardHook Pin
Eddy Vluggen4-Sep-15 7:00
professionalEddy Vluggen4-Sep-15 7:00 
GeneralRe: cant send key stroke only to Word Through my program that use KeyboardHook Pin
goldsoft4-Sep-15 21:35
goldsoft4-Sep-15 21:35 
GeneralRe: cant send key stroke only to Word Through my program that use KeyboardHook Pin
Eddy Vluggen5-Sep-15 0:45
professionalEddy Vluggen5-Sep-15 0:45 
QuestionC# Active Directory Pin
Vuyo884-Sep-15 0:47
Vuyo884-Sep-15 0:47 
AnswerRe: C# Active Directory Pin
Pete O'Hanlon4-Sep-15 0:58
mvePete O'Hanlon4-Sep-15 0:58 
AnswerRe: C# Active Directory Pin
Ravi Bhavnani4-Sep-15 7:25
professionalRavi Bhavnani4-Sep-15 7:25 
QuestionReceives a question mark when trying to see active window open in my program Pin
goldsoft3-Sep-15 21:59
goldsoft3-Sep-15 21:59 
AnswerRe: Receives a question mark when trying to see active window open in my program Pin
Eddy Vluggen3-Sep-15 23:15
professionalEddy Vluggen3-Sep-15 23:15 
QuestionReturn an image from database sql server management studio 2012 Pin
Member 119583513-Sep-15 17:54
Member 119583513-Sep-15 17:54 
AnswerRe: Return an image from database sql server management studio 2012 Pin
Wendelius3-Sep-15 18:20
mentorWendelius3-Sep-15 18:20 
AnswerRe: Return an image from database sql server management studio 2012 Pin
OriginalGriff3-Sep-15 22:07
mveOriginalGriff3-Sep-15 22:07 
QuestionFAXCOMEXLib need fax modem? Pin
Member 118422883-Sep-15 6:44
Member 118422883-Sep-15 6:44 
AnswerRe: FAXCOMEXLib need fax modem? Pin
OriginalGriff3-Sep-15 8:07
mveOriginalGriff3-Sep-15 8:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.