Click here to Skip to main content
15,894,539 members
Home / Discussions / C#
   

C#

 
QuestionDataGrid resizable with changing column numbers Pin
Calma11-Oct-06 8:45
Calma11-Oct-06 8:45 
QuestionKey Board Specifications (Key Codes) Pin
Syed Shahid Hussain11-Oct-06 7:45
Syed Shahid Hussain11-Oct-06 7:45 
AnswerRe: Key Board Specifications (Key Codes) Pin
led mike11-Oct-06 8:08
led mike11-Oct-06 8:08 
GeneralRe: Key Board Specifications (Key Codes) Pin
Dan Neely11-Oct-06 8:27
Dan Neely11-Oct-06 8:27 
GeneralRe: Key Board Specifications (Key Codes) Pin
Syed Shahid Hussain11-Oct-06 8:36
Syed Shahid Hussain11-Oct-06 8:36 
GeneralRe: Key Board Specifications (Key Codes) Pin
Dave Kreskowiak11-Oct-06 9:12
mveDave Kreskowiak11-Oct-06 9:12 
GeneralRe: Key Board Specifications (Key Codes) Pin
Dan Neely11-Oct-06 9:40
Dan Neely11-Oct-06 9:40 
GeneralRe: Key Board Specifications (Key Codes) Pin
Nader Elshehabi13-Oct-06 14:26
Nader Elshehabi13-Oct-06 14:26 
Hello Shahid

I know this reply came quite late, but here is a sample code for keyboard hooking. I didn't study it yet -I'm still stuck in my exams-, but I copied it from an earlier post for later revision. Maybe you can make your way through it:
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace GlobalKeyHook
{
  /// 
  /// KeyboardHook, as the name implies, hooks into the windows messaging system to
  /// catch global keystrokes.
  /// 
  /// 
  /// 	/// The hook is automatically released when the class is disposed.
  /// You can force the release by calling 
  /// The class exposes a KeyDown and KeyUp event, which can be used as any
  /// other keydown/up event in the .net framework. The sender object will always be
  /// this class, but all global keystrokes are processed.
  /// As in other keydown/up events, setting handled of the keyeventargs object to true
  /// e.Handled=true; will prevent other hooks from executing. For system keys
  /// as the Windows key, this means that the key gets 'blocked'
  /// 
  public class KeyboardHook : IDisposable
  {
    #region Hook-dlls

    [DllImport("user32", EntryPoint = "SetWindowsHookExA")]
    private static extern int SetWindowsHookEx(int idHook, KeyBoardCatcherDelegate lpfn, int hmod, int dwThreadId);

    [DllImport("user32.dll", CharSet = CharSet.Auto,
     ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
    private static extern short GetKeyState(int keyCode);

    [DllImport("user32", EntryPoint = "MapVirtualKey")]
    private static extern int MapVirtualKey(int wCode, int wMapType);

    [DllImport("user32", EntryPoint = "CallNextHookEx")]
    private static extern int CallNextHook(int hHook, int ncode, int wParam, KeybHookStruct lParam);

    private const int
      WH_KEYBOARD = 2,
      WH_KEYBOARD_LL = 13; //Global keyboard hook. 

    [DllImport("user32", EntryPoint = "UnhookWindowsHookEx")]
    private static extern int UnhookWindowsHookEx(int hHook);

    #endregion


    private bool m_DisableHooking = false;
    /// Gets/Sets the DisableHooking property.
    public bool DisableHooking
    {
      get { return m_DisableHooking; }
      set { m_DisableHooking = value; }
    }
	      

    public KeyboardHook()
    {
      kbcatcher = new KeyBoardCatcherDelegate(KeyBoardCatcher);

      keybhook = SetWindowsHookEx(WH_KEYBOARD_LL, kbcatcher,
        Marshal.GetHINSTANCE(typeof(KeyboardHook).Module).ToInt32(), 0);

      if (keybhook == 0)
        throw new ExternalException("Error: " + Marshal.GetLastWin32Error().ToString() + "\r\nCould not hook keyboard");
    }

    /// 
    /// See the property  for info
    /// 
    public KeyboardHook(bool AddModifierData)
      : this()
    {
      this.AddModifierData = AddModifierData;
    }

    #region Dispose
    /// The keyboard hook id, to be used with unhookwindowsex
    /// 
    private readonly int keybhook;

    private bool disposed;
    /// call Dispose when finished to release the hook.
    /// If this is not done manually, the destructor releases the hook,
    /// but manually disposing is more effective
    public void Dispose()
    {
      if (!disposed)
      {
        UnhookWindowsHookEx(keybhook);
        GC.SuppressFinalize(this);
        disposed = true;
      }
    }

    /// Destructor
    ~KeyboardHook()
    {
      Dispose();
    }
    #endregion

    /// This structure is filled when calling the KeyBoardCatcherDelegate.
    private struct KeybHookStruct
    {
      public int vkCode, scanCode, flags, time, dwExtraInfo;
    }

    private delegate int KeyBoardCatcherDelegate(int code, int wparam, ref KeybHookStruct lparam);


    public static bool CheckKeyPressed(params Keys[] keys)
    {
      for (int i = 0; i < keys.Length; i++)
        if (!CheckKeyPressed(ref keys[i])) return false;
      return true;
    }

    public static bool CheckKeyPressed(ref Keys key)
    {
      return CheckKeyPressed((int)key);
    }

    public static bool CheckKeyPressed(int vkey)
    {
      short ks = GetKeyState(vkey);
      Console.WriteLine(ks);
      return ks == 1;
    }

    public bool IsKeyPressed(Keys key)
    {
      return CheckKeyPressed(ref key);
    }

    public bool AreKeysPressed(params Keys[] keys)
    {
      return CheckKeyPressed(keys);
    }

    private const int HC_ACTION = 0;

    [MarshalAs(UnmanagedType.FunctionPtr)]
    private KeyBoardCatcherDelegate kbcatcher;

    /// Catch the keys if you like. These are the global keys. Set e.Handled=true to block the key (such as alt-tab)
    public event KeyEventHandler
      KeyDown = null,
      KeyUp = null;

    private const int
      wpKeyDown = 256,
      wpKeyUp = 257;

    /// If this value is true
    /// modifier data (such as shift,control,alt) is added to the KeyEventArgs, otherwise
    /// only the keydata is send.
    /// Adding the modifier data can produce some overhead since
    /// it is checked on every keypress.
    /// If you want to check manually, set this property to false
    /// and check  instead
    /// 
    public bool AddModifierData = true;

    public readonly Keys[] Modifiers = { Keys.Alt, Keys.Control, Keys.Shift };

    /// The method that's used to catch the keyboard input
    /// 
    /// 
    /// 
    /// 
    private int KeyBoardCatcher(int hookcode, int wparam, ref KeybHookStruct lparam)
    {
      if (HC_ACTION == hookcode)
      {
        if ((wparam == wpKeyDown && KeyDown != null)
          || (wparam == wpKeyUp && KeyUp != null))
        {
          try
          {
            Keys k = (Keys)lparam.vkCode;
            if (AddModifierData)
              k |= Control.ModifierKeys;

            KeyEventArgs e = new KeyEventArgs(k);

            if (wparam == wpKeyDown)
              KeyDown(this, e);
            else
              KeyUp(this, e);
            //If handled, do not process any other hooks after this one. (key is blocked)
            if (e.Handled) return 1;
          }
          /*
          //for debugging only
          catch(Exception ex)
          {
            MessageBox.Show(ex.Message);
          }
          */
          catch { }
        }
      }
      //Call the next hook for the input
      return CallNextHook(keybhook, hookcode, wparam, lparam);
    }
  }
}


RegardsRose | [Rose]

GeneralRe: Key Board Specifications (Key Codes) Pin
Syed Shahid Hussain13-Oct-06 20:47
Syed Shahid Hussain13-Oct-06 20:47 
GeneralRe: Key Board Specifications (Key Codes) Pin
Syed Shahid Hussain11-Oct-06 8:29
Syed Shahid Hussain11-Oct-06 8:29 
AnswerRe: Key Board Specifications (Key Codes) Pin
Amar Chaudhary11-Oct-06 15:17
Amar Chaudhary11-Oct-06 15:17 
Questiondatalist [modified] Pin
Ah_Mohsen_aly11-Oct-06 7:02
Ah_Mohsen_aly11-Oct-06 7:02 
AnswerRe: datalist Pin
Nader Elshehabi13-Oct-06 14:28
Nader Elshehabi13-Oct-06 14:28 
QuestionDICOM ... Pin
mostafa_h11-Oct-06 6:53
mostafa_h11-Oct-06 6:53 
Questioncsc usage Pin
waheed awan11-Oct-06 6:48
waheed awan11-Oct-06 6:48 
GeneralCrystal Report Problem Pin
Saleh Mahmoud11-Oct-06 6:31
Saleh Mahmoud11-Oct-06 6:31 
QuestionI want to use a SQL db local, in other computer Pin
giovannirodrigo11-Oct-06 6:06
giovannirodrigo11-Oct-06 6:06 
AnswerRe: I want to use a SQL db local, in other computer Pin
S. Senthil Kumar11-Oct-06 6:35
S. Senthil Kumar11-Oct-06 6:35 
GeneralRe: I want to use a SQL db local, in other computer Pin
giovannirodrigo16-Oct-06 2:31
giovannirodrigo16-Oct-06 2:31 
AnswerRe: I want to use a SQL db local, in other computer Pin
Dave Kreskowiak11-Oct-06 9:08
mveDave Kreskowiak11-Oct-06 9:08 
QuestionHow to syncrhonize a dialog edit ctrl with a web page edit ctrl Pin
kozu11-Oct-06 5:51
kozu11-Oct-06 5:51 
Questionitems in listbox Pin
fmardani11-Oct-06 5:35
fmardani11-Oct-06 5:35 
AnswerRe: items in listbox Pin
User 665811-Oct-06 6:27
User 665811-Oct-06 6:27 
AnswerRe: items in listbox Pin
Private_Void11-Oct-06 7:39
Private_Void11-Oct-06 7:39 
Questionchecking if value is one of a group Pin
impeham11-Oct-06 5:30
impeham11-Oct-06 5:30 

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.