-1

I am creating a program where I can increase / decrease my volume control using buttons on my keyboard. I have created the codes to increase / decrease the volume with my specified buttons however when I am active on another program, If I press the buttons on my keyboard they will not increase or decrease the volume. Now I need a code to keep my form always selected even if another form has been activated , I tried using Me.Topmost = true however it didn't work since the form is not selected with that code. I have spent hours trying to search for any hints but to no avail, however someone must know how to do this so any help is appreciated. - I am using Visual Basic 2012

  • 1
    If you do manage to find a way to direct all input to this application, how are you going to use any other applications? It seems that the problem you face is the logical opposite of the solution you propose. The problem is that you can't direct input to this application while using another application. Your intended solution is to force this application to be the only one which accepts input. This can be achieved by simply not using another application in the first place. – David Oct 20 '13 at 21:09
  • Google "vb.net registerhotkey example". – Hans Passant Oct 20 '13 at 21:12
  • spend a few currency units and buy a multimedia keyboard – Ňɏssa Pøngjǣrdenlarp Oct 20 '13 at 21:13
  • @David there should be a solution, nothing's impossible – user2901055 Oct 20 '13 at 21:18
  • @Hans Passant thanks i'l look into it now – user2901055 Oct 20 '13 at 21:19
  • @Plutonix I already have a keyboard with an increase / decrease volume button but I want to see if it is possible to create a program with the same settings. If your gonna spam do it elsewhere! – user2901055 Oct 20 '13 at 21:21

2 Answers2

1

What you need is a keyboard intercept. http://blogs.msdn.com/b/toub/archive/2006/05/03/589423.aspx has a working sample.

user2880486
  • 1,068
  • 7
  • 16
1

I think what you actually want is a "global keyboard hook". You'll need to add some code from the link below, but using it is simple...

To create the hook:

Private WithEvents kbHook As New KeyboardHook

Then each event can be handled:

Private Sub kbHook_KeyDown(ByVal Key As System.Windows.Forms.Keys) Handles kbHook.KeyDown
    Debug.WriteLine(Key.ToString) 
End Sub 
Private Sub kbHook_KeyUp(ByVal Key As System.Windows.Forms.Keys) Handles kbHook.KeyUp 
    Debug.WriteLine(Key) 
End Sub

Stolen from here: How to listen keyboard in background and fire keystrokes on demand?

Community
  • 1
  • 1
Derek Tomes
  • 3,989
  • 3
  • 27
  • 41
  • Thanks for your comment, now do you know where I should specify the button clicks? – user2901055 Oct 20 '13 at 21:24
  • If you use a keyboard hook it's not a button click, it'll be in the KeyDown, KeyPress or KeyUp events (as I listed above). – Derek Tomes Oct 20 '13 at 21:29
  • I have been trying to get the code to work for a while now but it won't work (the code on the other website) it gives me this error message: An unhandled exception of type 'System.StackOverflowException' occurred in System.Windows.Forms.dll at this code : Public Sub New() do you know how I can resolve this? Thanks – user2901055 Oct 20 '13 at 22:05
  • I suggest you start with the sample application and try to understand how it works. – Derek Tomes Oct 20 '13 at 22:17
  • Where is the sample application ? :/ – user2901055 Oct 20 '13 at 22:32
  • http://stackoverflow.com/questions/15038413/how-to-listen-keyboard-in-background-and-fire-keystrokes-on-demand – Derek Tomes Oct 20 '13 at 22:39
  • Yes that's where I recieve the error , (the code is on that site, could you possibly give it a go?) Thanks :) – user2901055 Oct 20 '13 at 22:51