473,789 Members | 2,500 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

keyboard hooks on a BHO

hi all,
i have implemented a keyboard hook which is active only when IE is
open. Its a BHO. I am adding the code below:
using System;
using System.Runtime. InteropServices ;
using System.Windows. Forms;
using System.Web.Serv ices;
using Microsoft.Win32 ;
using SHDocVw;

namespace KeyboardHooks_B HO
{

/// <summary>
/// Summary description for Class1.
/// </summary>
///
[ComVisible(true )]
[Guid("20C718C8-0D8C-42dc-B0CD-C7C53A23F116")]
public class BHO : IObjectWithSite
{
#region Member Variables
protected SHDocVw.IWebBro wser2 m_pIWebBrowser2 ; // the browser class
object
protected SHDocVw.DWebBro wserEvents2_Eve nt m_pDWebBrowserE vents2; //
browser events
// private ComImports KeyHook= new ComImports();

#endregion

#region Hook Variables

public struct KeyboardHookStr uct
{
public long vkCode;
public long scanCode;
public int flags;
public long time;
public long dwExtraInfo;

};
private const int HC_ACTION = 0;
private const int LLKHF_EXTENDED = 0x01;
private const int LLKHF_ALTDOWN = 0x20;
private const long VK_T = 0x54;
private const long VK_P = 0x50;
private const long VK_S = 0x53;
private const int WH_KEYBOARD_LL= 13;

protected IntPtr KeyboardHandle = IntPtr.Zero;
private static int mHook;
#endregion
public BHO()
{

MessageBox.Show ("inside BHO's constructor");
}
#region COM interfaces
#region IObjectWithSite Members

public void SetSite(object newSite)
{
MessageBox.Show ("inside SetSite");
object site=newSite;
string explorer= null;

if (m_pIWebBrowser 2!=null)
Release();

if( site != null )
Marshal.Release ComObject( site );

site = newSite;

if( site != null )
{
MessageBox.Show ("inside if site!=null");
IServiceProvide r sp = site as IServiceProvide r;
m_pIWebBrowser2 = site as SHDocVw.IWebBro wser2;
Guid guid = ExplorerGUIDs.I ID_IWebBrowserA pp;
Guid riid = ExplorerGUIDs.I ID_IUnknown;

object wba;
sp.QueryService (ref guid,ref riid,out wba );

/*explorer = (WebBrowserClas s)Marshal.Creat eWrapperOfType(
wba as SHDocVw.IWebBro wser2,
typeof(WebBrows erClass));*/

explorer=m_pIWe bBrowser2.FullN ame.ToUpper();

if(explorer.End sWith("IEXPLORE .EXE"))
{
MessageBox.Show ("inside the IEXPLORE.EXE");
// KeyHook.Install ();
Install();
MessageBox.Show ("hooking");

}
}
else
{
if ( explorer != null )
{
IServiceProvide r sp = site as IServiceProvide r;
m_pIWebBrowser2 = site as SHDocVw.IWebBro wser2;
Guid guid = ExplorerGUIDs.I ID_IWebBrowserA pp;
Guid riid = ExplorerGUIDs.I ID_IUnknown;

object wba;
sp.QueryService (ref guid,ref riid,out wba );

explorer=m_pIWe bBrowser2.FullN ame.ToUpper();
//Marshal.Release ComObject( explorer );

//explorer is about to close, freeing resources
if(explorer.End sWith("IEXPLORE .EXE"))
{
//KeyHook.UnInsta ll();
UnInstall();
MessageBox.Show ("UnHookMe") ;
}

explorer = null;
}
MessageBox.Show ("end of setsite");
return;
}

}

public int GetSite(ref Guid guid, out IntPtr ppvSite)
{
// TODO: Add BHO.GetSite implementation
MessageBox.Show ("inside getsite");
ppvSite = new IntPtr ();
return 0;
}

#endregion

protected void Release()
{
if (m_pDWebBrowser Events2!=null)
{
Marshal.Release ComObject(m_pDW ebBrowserEvents 2);
m_pDWebBrowserE vents2=null;
}

if (m_pIWebBrowser 2 != null)
{
Marshal.Release ComObject(m_pIW ebBrowser2);
m_pIWebBrowser2 = null;
}
}

#endregion

#region Hook Functions

private delegate int KeyboardHookPro cDelegate(int nCode, int wParam,
int lParam);

public void Install()
{
MessageBox.Show ("inside Install");
mHook=SetWindow sHookEx(WH_KEYB OARD_LL,new
KeyboardHookPro cDelegate(Keybo ardHookProc),In tPtr.Zero,0);
if(mHook!=0)
{
MessageBox.Show ("unable to install");
}
else
MessageBox.Show ("Success in Installing!");
}
public static int KeyboardHookPro c(int nCode, int wParam, int lParam)

{
KeyboardHookStr uct HookStruct;
int ret = 0;
MessageBox.Show ("inside HookProc");

HookStruct = (KeyboardHookSt ruct) Marshal.PtrToSt ructure(new
IntPtr(lParam), typeof(Keyboard HookStruct));
long vkCode= HookStruct.vkCo de;
int flag = HookStruct.flag s;

if(nCode==HC_AC TION)
{
MessageBox.Show ("inside nCode==HC_ACTIO N");
//System.Windows. Forms.Keys KeyPressed = (Keys)wParam.To Int32();

// Insert + T OR Alt + T
if(vkCode == VK_T )
{
if(flag==LLKHF_ EXTENDED)
{
MessageBox.Show ("Play");
ret=1;
}
else if((flag & LLKHF_ALTDOWN)! =0)
{
MessageBox.Show ("Stop");
ret=1;
}
}
else if((vkCode == VK_P)&&((flag & LLKHF_EXTENDED) !=0))
{ // Insert + P
MessageBox.Show ("pause");
ret = 1 ;
}
else if((vkCode == VK_S) && ((flag & LLKHF_ALTDOWN)! =0))
{ // Alt + S
MessageBox.Show ("Save");
ret = 1 ;
}

/*/ CopyMemory(Hook Struct,lParam,s izeof(HookStruc t);
if(IsHooked(Hoo kStruct))
{
MyKeyboardProc= 1;
}*/

// MyKeyboardProc= 1;
}
if( ret == 0 )
{
ret = CallNextHookEx( mHook,nCode,wPa ram,lParam);
}
return ret;
}
public void UnInstall()
{
MessageBox.Show ("inside UnInstall");
if(mHook!=0)
{
MessageBox.Show ("inside mHook!=0 ");
UnhookWindowsHo okEx(KeyboardHa ndle);
}
}
#endregion

#region Hook Win32Imports

// Win32: SetWindowsHookE x()
[DllImport("user 32.dll")]
private static extern int SetWindowsHookE x(int code,
KeyboardHookPro cDelegate func,
IntPtr hInstance,
int threadID);

// Win32: UnhookWindowsHo okEx()
[DllImport("user 32.dll")]
protected static extern int UnhookWindowsHo okEx(IntPtr hhook);
// Win32: CallNextHookEx( )
[DllImport("user 32.dll")]
protected static extern int CallNextHookEx( int hhook,
int code, int wParam, int lParam);
#endregion
}
}
the questions are:
1.the BHO is working fine, however, though SetWindowsHookE x getting
executed successfully,.. none of my hooks are working...
2. I guess the virtual keys are having some kind of problem. I dont
know how to solve this.
3.also my hook procedure is not getting called. y?
hope u is clear.
waiting for a clarification
thanks and regards,
hema

Apr 7 '06 #1
0 2612

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
13036
by: daFritz | last post by:
Hi! I need to prevent Task-Switching in my App, so I tried to implement a low level keyboard hook in a extra class. But it seems that the Parameters are not passed correctly to my Hook function. Similar Examples in VB .NET work correctly. Heres the Source Code: using System; using System.Runtime.InteropServices;
1
3133
by: Salim Ansari via .NET 247 | last post by:
(Type your message here) -------------------------------- From: Salim Ansari Can anybody please give me sample code in C# to use keyboard hooks in C#? THat would be a great help. Thanks in advance Salim -----------------------
2
5434
by: Christoph Brüser | last post by:
Hi, in my application I want to react to certain keys when a context menu is showing. So I installed a keyboard hook, but now whenever a key is pressed when the menu is showing, the application crashes and sometimes throws a NullReferenceException in system.windows.forms.dll. I assume that the hook somehow messes up the context menu's message loop or something like this. I also had this problem with a mouse hook, so i guess its a...
2
3533
by: genojoe | last post by:
I have a Windows form application running that contains a button and textbox. An easy thing to do is to monitor the last time a keystroke was executed in the textbox. I can then click the button and it will say: "The last keystroke in the textbox was 10 minutes ago." This is not what I want to do. What I would like to see is a message that says: "The last keystroke in any running application on this computer was 23 minutes ago." My...
7
26543
by: jpierson | last post by:
Hi, I am tryin to create a keyboard hook that sends the keystroke ctrl + pause/break. I haven't used keyboard hooks before so I'm not too sure how to use them public int MyKeyboardProc(int nCode, int wParam, int lParam) {
13
3199
by: Hema | last post by:
hello all, I am working on a project related to Internet Explorer. I want my application to be invoked by a keypress( single key stroke or a combination). But this must get invoked only when the IE browser is open. I learnt that SetWindowsHookEx must be used along with few others. But my doubt is how to call my application? Can i implement the keyboard hooks related methods in the same project as my application or should this be else...
0
1419
by: priya | last post by:
hello, I want to add a keyboard hook to my application.This should be detected only from IE.I searched on this and found the IEAccessible_final_sem_doc in the following link: http://slappy.cs.uiuc.edu/fall03/team2/Final/index.html I understand that i have to install the hooks using the SetWindowsHookEx from within the IObjectWithSite:SetSite. But i came across this line in the code...
5
1560
by: Hema | last post by:
Hi all, I want to develop a application that logs the keys pressed only when IE is active(i.e. in focus).The hook must not log any key press when any windows or applications are in focus.How to do this? Thanks and regards, hema
9
10981
by: Kbalz | last post by:
I have an application that minimizes itself, and I want it to listen for certain key commands, and when they are pressed, my program can react to them. So far I've gotten my application to react as I intend while the program has focus, but when the application loses focus, the listening stops. I am using VS 2008 beta 2, with .net 3.5.
0
9663
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10195
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10136
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9016
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6765
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5548
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4090
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.