Disable a key

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 11143 Views - Last Post: 17 April 2009 - 09:58 AM Rate Topic: -----

#1 paul_so40   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 15-April 09

Disable a key

Posted 15 April 2009 - 05:33 PM

Ok, so im a bit new to vb and need some help.

Im looking for the code to dsable a key in form load.

so basicaly, if at any point, wile the program is loaded if the key
'
is pressed then nothing happens.

ive done my homework and found out this would have to be done using a low level keyboard hook.

If someone could set me of in the right direction it would be great.

Thanks

Paul

Is This A Good Question/Topic? 0
  • +

Replies To: Disable a key

#2 jimdandy75   User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 311
  • Joined: 30-June 08

Re: Disable a key

Posted 15 April 2009 - 05:47 PM

See if this post is helpful
Click here ------
Was This Post Helpful? 0
  • +
  • -

#3 paul_so40   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 15-April 09

Re: Disable a key

Posted 15 April 2009 - 05:54 PM

Hi, Yes, but thats for disabling it for the form.

What i want to do is disable it globaly. including other softwares like in notepad, outlook, msn etc

I think this needs to be done using a low level keyboard hook.

Anyone got an example code i could use?

This post has been edited by paul_so40: 15 April 2009 - 05:55 PM

Was This Post Helpful? 0
  • +
  • -

#4 AdmSteck   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 61
  • Joined: 26-November 05

Re: Disable a key

Posted 15 April 2009 - 05:56 PM

View Postpaul_so40, on 15 Apr, 2009 - 04:33 PM, said:

Ok, so im a bit new to vb and need some help.

Im looking for the code to dsable a key in form load.

so basicaly, if at any point, wile the program is loaded if the key
'
is pressed then nothing happens.

ive done my homework and found out this would have to be done using a low level keyboard hook.

If someone could set me of in the right direction it would be great.

Thanks

Paul


Easiest method would be to disable the button so the user can't even click it with ButtonName.Enabled = False. You could also remove the event handler for the button click event at run time and add it back later if needed.
Was This Post Helpful? 0
  • +
  • -

#5 Ian2009   User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 31
  • Joined: 07-April 09

Re: Disable a key

Posted 15 April 2009 - 05:59 PM

View Postpaul_so40, on 15 Apr, 2009 - 04:33 PM, said:

Ok, so im a bit new to vb and need some help.

Im looking for the code to dsable a key in form load.

so basicaly, if at any point, wile the program is loaded if the key
'
is pressed then nothing happens.

ive done my homework and found out this would have to be done using a low level keyboard hook.

If someone could set me of in the right direction it would be great.

Thanks

Paul


set form's KeyPreview to True and in Form1_KeyPress event handler, you can do one of the following two things
If e.KeyChar = "'" Then e.Handled = True

or
If e.KeyChar = "'" Then e.KeyChar = Nothing

Was This Post Helpful? 1
  • +
  • -

#6 paul_so40   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 15-April 09

Re: Disable a key

Posted 15 April 2009 - 06:06 PM

Quote

set form's KeyPreview to True and in Form1_KeyPress event handler, you can do one of the following two things
If e.KeyChar = "'" Then e.Handled = True

or
If e.KeyChar = "'" Then e.KeyChar = Nothing


Hi, and thanks for the reply, How could i make it do nothing, but not just in my program but globaly? e.g. in outlook, msn, notepad etc

?

Thanks
Was This Post Helpful? 0
  • +
  • -

#7 Ian2009   User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 31
  • Joined: 07-April 09

Re: Disable a key

Posted 15 April 2009 - 07:29 PM

View Postpaul_so40, on 15 Apr, 2009 - 05:06 PM, said:

Quote

set form's KeyPreview to True and in Form1_KeyPress event handler, you can do one of the following two things
If e.KeyChar = "'" Then e.Handled = True

or
If e.KeyChar = "'" Then e.KeyChar = Nothing


Hi, and thanks for the reply, How could i make it do nothing, but not just in my program but globaly? e.g. in outlook, msn, notepad etc

?

Thanks

check this out. hope it helps.
http://www.developer...11087_2193301_1
Was This Post Helpful? 0
  • +
  • -

#8 paul_so40   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 15-April 09

Re: Disable a key

Posted 16 April 2009 - 07:22 AM

Imports System.Runtime.InteropServices
Imports System.Reflection
Imports System.Drawing
Imports System.Threading

Module Keyboard
	Public Declare Function UnhookWindowsHookEx Lib "user32" _
	  (ByVal hHook As Integer) As Integer

	Public Declare Function SetWindowsHookEx Lib "user32" _
	  Alias "SetWindowsHookExA" (ByVal idHook As Integer, _
	  ByVal lpfn As KeyboardHookDelegate, ByVal hmod As Integer, _
	  ByVal dwThreadId As Integer) As Integer

	Private Declare Function GetAsyncKeyState Lib "user32" _
	  (ByVal vKey As Integer) As Integer

	Private Declare Function CallNextHookEx Lib "user32" _
	  (ByVal hHook As Integer, _
	  ByVal nCode As Integer, _
	  ByVal wParam As Integer, _
	  ByVal lParam As KBDLLHOOKSTRUCT) As Integer

	Public Structure KBDLLHOOKSTRUCT
		Public vkCode As Integer
		Public scanCode As Integer
		Public flags As Integer
		Public time As Integer
		Public dwExtraInfo As Integer
	End Structure

	' Low-Level Keyboard Constants
	Private Const HC_ACTION As Integer = 0
	Private Const LLKHF_EXTENDED As Integer = &H1
	Private Const LLKHF_INJECTED As Integer = &H10
	Private Const LLKHF_ALTDOWN As Integer = &H20
	Private Const LLKHF_UP As Integer = &H80

	' Virtual Keys
	Public Const VK_TAB = &H9
	Public Const VK_CONTROL = &H11
	Public Const VK_ESCAPE = &H1B
	Public Const VK_DELETE = &H2E

	Private Const WH_KEYBOARD_LL As Integer = 13&
	Public KeyboardHandle As Integer


	' Implement this function to block as many
	' key combinations as you'd like
	Public Function IsHooked( _
	  ByRef Hookstruct As KBDLLHOOKSTRUCT) As Boolean

		Debug.WriteLine("Hookstruct.vkCode: " & Hookstruct.vkCode)
		Debug.WriteLine(Hookstruct.vkCode = VK_ESCAPE)
		Debug.WriteLine(Hookstruct.vkCode = VK_TAB)

		If (Hookstruct.vkCode = VK_ESCAPE) And _
		  CBool(GetAsyncKeyState(VK_CONTROL) _
		  And &H8000) Then

			Call HookedState("Ctrl + Esc blocked")
			Return True
		End If

		If (Hookstruct.vkCode = VK_TAB) And _
		  CBool(Hookstruct.flags And _
		  LLKHF_ALTDOWN) Then

			Call HookedState("Alt + Tab blockd")
			Return True
		End If

		If (Hookstruct.vkCode = VK_ESCAPE) And _
		  CBool(Hookstruct.flags And _
			LLKHF_ALTDOWN) Then

			Call HookedState("Alt + Escape blocked")
			Return True
		End If

		Return False
	End Function

	Private Sub HookedState(ByVal Text As String)
		Debug.WriteLine(Text)
	End Sub

	Public Function KeyboardCallback(ByVal Code As Integer, _
	  ByVal wParam As Integer, _
	  ByRef lParam As KBDLLHOOKSTRUCT) As Integer

		If (Code = HC_ACTION) Then
			Debug.WriteLine("Calling IsHooked")

			If (IsHooked(lParam)) Then
				Return 1
			End If

		End If

		Return CallNextHookEx(KeyboardHandle, _
		  Code, wParam, lParam)

	End Function


	Public Delegate Function KeyboardHookDelegate( _
	  ByVal Code As Integer, _
	  ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) _
				   As Integer

	<MarshalAs(UnmanagedType.FunctionPtr)> _
	Private callback As KeyboardHookDelegate

	Public Sub HookKeyboard()
		callback = New KeyboardHookDelegate(AddressOf KeyboardCallback)

		KeyboardHandle = SetWindowsHookEx( _
		  WH_KEYBOARD_LL, callback, _
		  Marshal.GetHINSTANCE( _
		  [Assembly].GetExecutingAssembly.GetModules()(0)).ToInt32, 0)

		Call CheckHooked()
	End Sub

	Public Sub CheckHooked()
		If (Hooked()) Then
			Debug.WriteLine("Keyboard hooked")
		Else
			Debug.WriteLine("Keyboard hook failed: " & Err.LastDllError)
		End If
	End Sub

	Private Function Hooked()
		Hooked = KeyboardHandle <> 0
	End Function

	Public Sub UnhookKeyboard()
		If (Hooked()) Then
			Call UnhookWindowsHookEx(KeyboardHandle)
		End If
	End Sub

End Module



o i need to modify this to block the

Quote

'
key, any ideas how ?

i take it its this bit im mofying ?

 Public Function IsHooked( _
	  ByRef Hookstruct As KBDLLHOOKSTRUCT) As Boolean

		Debug.WriteLine("Hookstruct.vkCode: " & Hookstruct.vkCode)
		Debug.WriteLine(Hookstruct.vkCode = VK_ESCAPE)
		Debug.WriteLine(Hookstruct.vkCode = VK_TAB)

		If (Hookstruct.vkCode = VK_ESCAPE) And _
		  CBool(GetAsyncKeyState(VK_CONTROL) _
		  And &H8000) Then

			Call HookedState("Ctrl + Esc blocked")
			Return True
		End If

		If (Hookstruct.vkCode = VK_TAB) And _
		  CBool(Hookstruct.flags And _
		  LLKHF_ALTDOWN) Then

			Call HookedState("Alt + Tab blockd")
			Return True
		End If

		If (Hookstruct.vkCode = VK_ESCAPE) And _
		  CBool(Hookstruct.flags And _
			LLKHF_ALTDOWN) Then

			Call HookedState("Alt + Escape blocked")
			Return True
		End If

		Return False
	End Function

Was This Post Helpful? 0
  • +
  • -

#9 paul_so40   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 15-April 09

Re: Disable a key

Posted 16 April 2009 - 11:08 AM

Any ideas what the vk code is for a single quote?
Was This Post Helpful? 0
  • +
  • -

#10 microstoc   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 59
  • Joined: 04-April 09

Re: Disable a key

Posted 16 April 2009 - 02:12 PM

View Postpaul_so40, on 16 Apr, 2009 - 10:08 AM, said:

Any ideas what the vk code is for a single quote?


It is not listed in the Win32Api.Txt file as one of the standard microsoft constants.
Sun list it as 0xDE = 222.
I thought a quote was Shift+2 or are you on some American keyboard . Can you test for these two keys in that hook code?
Was This Post Helpful? 0
  • +
  • -

#11 paul_so40   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 15-April 09

Re: Disable a key

Posted 17 April 2009 - 02:26 AM

View Postmicrostoc, on 16 Apr, 2009 - 01:12 PM, said:

View Postpaul_so40, on 16 Apr, 2009 - 10:08 AM, said:

Any ideas what the vk code is for a single quote?


It is not listed in the Win32Api.Txt file as one of the standard microsoft constants.
Sun list it as 0xDE = 222.
I thought a quote was Shift+2 or are you on some American keyboard . Can you test for these two keys in that hook code?


No, i am on a uk qwerty keyboard, above 2 is a

Quote

"
and the

Quote

'
is on the same key as the

Quote

@
key
Was This Post Helpful? 0
  • +
  • -

#12 microstoc   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 59
  • Joined: 04-April 09

Re: Disable a key

Posted 17 April 2009 - 06:21 AM

View Postpaul_so40, on 17 Apr, 2009 - 01:26 AM, said:

View Postmicrostoc, on 16 Apr, 2009 - 01:12 PM, said:

View Postpaul_so40, on 16 Apr, 2009 - 10:08 AM, said:

Any ideas what the vk code is for a single quote?


It is not listed in the Win32Api.Txt file as one of the standard microsoft constants.
Sun list it as 0xDE = 222.
I thought a quote was Shift+2 or are you on some American keyboard . Can you test for these two keys in that hook code?


No, i am on a uk qwerty keyboard, above 2 is a

Quote

"
and the

Quote

'
is on the same key as the

Quote

@
key


Well you live and learn. I've always considered ["] to be a quote, and I have always called ['] an Apostrophe. Now I know it's a single quote. Ha. All I got is VK_OEM_7 0xDE OEM_7 (" ') US
Was This Post Helpful? 0
  • +
  • -

#13 paul_so40   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 15-April 09

Re: Disable a key

Posted 17 April 2009 - 06:51 AM

Quote

Well you live and learn. I've always considered ["] to be a quote, and I have always called ['] an Apostrophe. Now I know it's a single quote. Ha. All I got is VK_OEM_7 0xDE OEM_7 (" ') US


Yh, it is also known as a Apostrophe, just for the sake of simplicity, most people will call it a single quote. :)

now i know the vk code, im still a little stuck.

I am trying to use the source code found at

http://www.planet-so...p...2&lngWId=10

If someone could download it it, and show me what to modify, to get it to work, it would be a great help and put my mind at rest.

Thanks

Paul
Was This Post Helpful? 0
  • +
  • -

#14 Asscotte   User is offline

  • D.I.C Addict
  • member icon

Reputation: 35
  • View blog
  • Posts: 610
  • Joined: 08-February 09

Re: Disable a key

Posted 17 April 2009 - 07:56 AM

What dont you like about the " ' " key? It never hurt you *sob* :D why dont you just intercept it rather than stop it?
Was This Post Helpful? 0
  • +
  • -

#15 paul_so40   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 15-April 09

Re: Disable a key

Posted 17 April 2009 - 08:03 AM

View PostAsscotte, on 17 Apr, 2009 - 06:56 AM, said:

What dont you like about the " ' " key? It never hurt you *sob* :D why dont you just intercept it rather than stop it?


lol, keyboard it broke, and it randomly just decides to give out ''''''''''' all the time wich can g'''e'''t''''''''''ve'''r'''y'''e''n'''o''''y''i'''n'''g' and is built into the laptop

I dont know how to just intercept it, if you could give me an example script i could use that will stop the ' key, then id hapily use that instead.

Thanks
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2