9 Replies - 7049 Views - Last Post: 07 July 2010 - 03:58 AM Rate Topic: -----

#1 Pestakop  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-July 10

VB.NET Keyboard Shortcut

Posted 06 July 2010 - 05:20 AM

Hello.

I'm currently a mind game project right now. But i need little help

First of all i think i have to explain the project.

Its a researching game with google.

Player looks first clue from my Form1 and searchs on the internet.When he find a new clue for lvl2 he Press for example(TAB) Button and form2 appears.

So i need a Key Shourtcut,when i on any other windows application.

Thanks for your helps.

Is This A Good Question/Topic? 0
  • +

Replies To: VB.NET Keyboard Shortcut

#2 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4531
  • View blog
  • Posts: 7,903
  • Joined: 08-June 10

Re: VB.NET Keyboard Shortcut

Posted 06 July 2010 - 07:28 AM

You can use the KeyDown event of whatever control it is you want the shortcut to be attached to, including the form itself. Just test for the proper key/combo.
Was This Post Helpful? 1
  • +
  • -

#3 Pestakop  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-July 10

Re: VB.NET Keyboard Shortcut

Posted 06 July 2010 - 07:41 AM

I understand KeyDown event but i think i have to focus my program to WebBrowser i need that. Cause my program will be hidden. Just MsgBoxes. For example I need to get msgbox from my program when i on IExplorer.

This post has been edited by Pestakop: 06 July 2010 - 07:46 AM

Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4531
  • View blog
  • Posts: 7,903
  • Joined: 08-June 10

Re: VB.NET Keyboard Shortcut

Posted 06 July 2010 - 08:26 AM

Ok, you're going to have to look at adding a hook into your application. I'll be writing a full article on this soon, but until then, take a look at this sample code, and adapt it to what you need.

Imports System.Runtime.InteropServices

Public Class Form1
    'Defines all our constants.  
    Public Const ALT As Integer = 1
    Public Const CTRL As Integer = 2
    Public Const SHIFT As Integer = 4
    Public Const WIN As Integer = 8
    Public Const HK_MSG_ID As Integer = &H312

    'The loading event.  We register our hotkey here.
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If AddHotKey(ALT, "O"c) Then 'First, make sure registration was successful
            TextBox1.Text &= "Hotkey ALT+O Registered" & Environment.NewLine
        Else
            TextBox1.Text &= "Hotkey Registration Failed" & Environment.NewLine
        End If
    End Sub

    'make sure to unregister the hotkey when we exit
    Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        RemoveHotKey()
    End Sub

    'a wrapper function to make registering the hotkey easier for us
    Private Function AddHotKey(ByVal modifier As Integer, ByVal keyCharacter As Char) As Boolean
        Return RegisterHotKey(Me.Handle, Me.GetHashCode(), modifier, Asc(Char.ToUpper(keyCharacter)))
    End Function

    'a wrapper to make removing the hotkey simple
    Private Function RemoveHotKey() As Boolean
        Return UnregisterHotKey(Me.Handle, Me.GetHashCode())
    End Function

    'the method we want to call when our hotkey is pressed
    Private Sub HandleHotKey()
        TextBox1.Text &= "Hotkey Pressed" & Environment.NewLine
    End Sub

    'the method that processes the windows messages.
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If m.Msg = HK_MSG_ID Then 'check to see if it is the hotkey message
            HandleHotKey() ' if so, handle it
        End If
        MyBase.WndProc(m) 'always call the base WndProc method
    End Sub

    'Import the hotkey registering functions from the user32 dll.
    <DllImport("user32.dll")>
    Public Shared Function RegisterHotKey(ByVal hWnd As IntPtr, ByVal id As Integer, ByVal fsModifier As Integer, ByVal vlc As Integer) As Boolean
    End Function
    <DllImport("user32.dll")>
    Public Shared Function UnregisterHotKey(ByVal hWnd As IntPtr, ByVal id As Integer) As Boolean
    End Function
End Class

Was This Post Helpful? 2
  • +
  • -

#5 Pestakop  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-July 10

Re: VB.NET Keyboard Shortcut

Posted 06 July 2010 - 08:32 AM

Thank you so much :)
Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4531
  • View blog
  • Posts: 7,903
  • Joined: 08-June 10

Re: VB.NET Keyboard Shortcut

Posted 06 July 2010 - 08:40 AM

Remember to push the green button at the bottom of helpful posts =D
Was This Post Helpful? 1
  • +
  • -

#7 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 183
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Re: VB.NET Keyboard Shortcut

Posted 06 July 2010 - 02:23 PM

Alternatively you could just use GetAsyncKeyState...

http://www.pinvoke.n...etasynckeystate
Was This Post Helpful? 1
  • +
  • -

#8 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4531
  • View blog
  • Posts: 7,903
  • Joined: 08-June 10

Re: VB.NET Keyboard Shortcut

Posted 06 July 2010 - 02:42 PM

I guess it's the difference between event driven programming and procedural. RegisterHotKey doesn't involve a timer ticking frequently to check a key's state. It just catches the windows message and handles it accordingly. GetAsyncKeyState involves you repeatedly checking the state (or recent state) of a single key, not a combination.

Either way works if you're looking to catch one key, with RegisterHotKey likely being more efficient. RegisterHotKey is easier if you're trying to register a combo. GetAsyncKeyState would probably work better if you're trying to catch more than one non-modifier key, as RegisterHotKey is unable to use more than one non-modifier key.
Was This Post Helpful? 1
  • +
  • -

#9 Pestakop  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-July 10

Re: VB.NET Keyboard Shortcut

Posted 07 July 2010 - 12:08 AM

I'm really appreciate about your answers. Can i do this with RegisterHotkey ?

Somebody told me RegisterHotkey thing works every windows application ?
Was This Post Helpful? 0
  • +
  • -

#10 Pestakop  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-July 10

Re: VB.NET Keyboard Shortcut

Posted 07 July 2010 - 03:58 AM

Problem Solved With RegisterHotkey Thank You.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1