Jump to content
  • Advertisement
Dr. David Bowman

C# Newbie's Newbie Trying to get KeyEventArgs in C# to Work

Recommended Posts

You forgot to copy the code from your previous post where the kbh_OnKeyPressed/kbh_OnKeyUnpressed functions are defined. Add the code block you have missed into

public class Program
{
  ...
}

definition and anything should work fine.

This is also what the compiler is telling you in the error messages, that you used a function that wasn't defined. I suggest doing some C# tutorials and learn how to read compiler messages 😊

Share this post


Link to post
Share on other sites
Advertisement

Dear Sir,

Thank you very much for your interest in my Thread and also for the kind advice

I have managed to combined everything into 1 .CS File as shown below

I have managed to get the whole code down to only 1 Compile Error Message

There are no Warning Messages too

The 1 Error Message is this 

Error Message - Error CS5001 Program does not contain a static 'Main' method suitable for an entry point WindowsFormsApp3

Can someone please kindly help me out on this 

Hope to hear from you soon

Thank you very much for your time and kind help

Kind Regards as Always Sir

//Error Message - Error	CS5001	Program does not contain a static 'Main' method suitable for an entry point	WindowsFormsApp3

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Text;
using System.Diagnostics;

public class Program
{
    bool lctrlKeyPressed;
    bool f1KeyPressed;

    [STAThread]
    public void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        LowLevelKeyboardHook kbh = new LowLevelKeyboardHook();
        kbh.OnKeyPressed += kbh_OnKeyPressed;
        kbh.OnKeyUnpressed += kbh_OnKeyUnPressed;

        kbh.HookKeyboard();

        Application.Run();

        kbh.UnHookKeyboard();
    }

    void kbh_OnKeyPressed(object sender, Keys e)
    {
        if (e == Keys.LControlKey)
        {
            lctrlKeyPressed = true;
        }
        else if (e == Keys.F1)
        {
            f1KeyPressed = true;
        }
        CheckKeyCombo();
    }

    void kbh_OnKeyUnPressed(object sender, Keys e)
    {
        if (e == Keys.LControlKey)
        {
            lctrlKeyPressed = false;
        }
        else if (e == Keys.F1)
        {
            f1KeyPressed = false;
        }
    }

    void CheckKeyCombo()
    {
        if (lctrlKeyPressed && f1KeyPressed)
        {
            //Open Form
        }
    }
}

public class LowLevelKeyboardHook
{
    private const int WH_KEYBOARD_LL = 13;
    private const int WM_KEYDOWN = 0x0100;
    private const int WM_SYSKEYDOWN = 0x0104;
    private const int WM_KEYUP = 0x101;
    private const int WM_SYSKEYUP = 0x105;

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool UnhookWindowsHookEx(IntPtr hhk);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);

    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr GetModuleHandle(string lpModuleName);

    public delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);

    public event EventHandler<Keys> OnKeyPressed;
    public event EventHandler<Keys> OnKeyUnpressed;

    private LowLevelKeyboardProc _proc;
    private IntPtr _hookID = IntPtr.Zero;

    public LowLevelKeyboardHook()
    {
        _proc = HookCallback;
    }

    public void HookKeyboard()
    {
        _hookID = SetHook(_proc);
    }

    public void UnHookKeyboard()
    {
        UnhookWindowsHookEx(_hookID);
    }

    private IntPtr SetHook(LowLevelKeyboardProc proc)
    {
        using (Process curProcess = Process.GetCurrentProcess())
        using (ProcessModule curModule = curProcess.MainModule)
        {
            return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
        }
    }

    private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
    {
        if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN || wParam == (IntPtr)WM_SYSKEYDOWN)
        {
            int vkCode = Marshal.ReadInt32(lParam);

            OnKeyPressed.Invoke(this, ((Keys)vkCode));
        }
        else if (nCode >= 0 && wParam == (IntPtr)WM_KEYUP || wParam == (IntPtr)WM_SYSKEYUP)
        {
            int vkCode = Marshal.ReadInt32(lParam);

            OnKeyUnpressed.Invoke(this, ((Keys)vkCode));
        }

        return CallNextHookEx(_hookID, nCode, wParam, lParam);
    }
}

 

 

 

Share this post


Link to post
Share on other sites
On 8/12/2019 at 6:16 PM, Dr. David Bowman said:

Error Message - Error CS5001 Program does not contain a static 'Main' method suitable for an entry point WindowsFormsApp3

As I mentioned, learn to read your compiler's messages properly 😉

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!