Active process and shortcut keys

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rotsey

    #1

    Active process and shortcut keys

    Hi,

    I am writing app that I want to be able to hit a shortcut
    key (windows wide) and have it all launch my app and then
    be able to get the name of the process that was active when
    the key was pressed.

    Is this possible and how?

    I know I can assign a shortcut key to the app but how do
    I get the active process name?

    rotsey


  • Newbie Coder

    #2
    Re: Active process and shortcut keys

    G'day Malcolm,

    This is an example to register the current app to be launched via a global
    hotkey. Adapt it for your own purposes.

    If you notice atom name is the name of this application which you need to
    change. As you're hard coding you will have the process name too

    Remember to pass the process name to the application for unregistering the
    hotkey

    Imports System.Runtime. InteropServices

    #Region "Declaratio ns"

    Private Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As IntPtr,
    ByVal id As _
    Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer
    Private Declare Function UnregisterHotKe y Lib "user32" (ByVal hwnd As
    IntPtr, ByVal id _
    As Integer) As Integer
    Private Declare Function GlobalAddAtom Lib "kernel32" Alias "GlobalAddAtomA "
    (ByVal _
    lpString As String) As Short
    Private Declare Function GlobalDeleteAto m Lib "kernel32" (ByVal nAtom As
    Short) As Short

    Private Const MOD_ALT As Integer = 1
    Private Const MOD_CONTROL As Integer = 2
    Private Const MOD_SHIFT As Integer = 4
    Private Const MOD_WIN As Integer = 8

    Dim hotkeyID As Short

    #End Region

    #Region "Register Global HotKey (SUB)"

    ' register a global hot key
    Private Sub RegisterGlobalH otKey(ByVal hotkey As Keys, ByVal modifiers As
    Integer)
    Try
    ' use the GlobalAddAtom API to get a unique ID (as suggested by MSDN
    docs)
    Dim atomName As String = AppDomain.GetCu rrentThreadId.T oString("X8")
    & Me.Name
    hotkeyID = GlobalAddAtom(a tomName)
    If hotkeyID = 0 Then
    Throw New Exception("Unab le to generate unique hotkey ID. Error
    code: " & _
    Marshal.GetLast Win32Error().To String)
    End If

    ' register the hotkey, throw if any error
    If RegisterHotKey( Me.Handle, hotkeyID, modifiers, CInt(hotkey)) = 0
    Then
    Throw New Exception("Unab le to register hotkey. Error code: " &
    _
    Marshal.GetLast Win32Error.ToSt ring)
    End If
    Catch ex As Exception
    ' clean up if hotkey registration failed
    UnregisterGloba lHotKey()
    End Try
    End Sub

    #End Region

    #Region "Unregister Global HotKey (SUB)"

    ' unregister a global hotkey
    Private Sub UnregisterGloba lHotKey()
    If Me.hotkeyID <0 Then
    UnregisterHotKe y(Me.Handle, hotkeyID)
    ' clean up the atom list
    GlobalDeleteAto m(hotkeyID)
    hotkeyID = 0
    End If
    End Sub

    #End Region

    Useage:

    RegisterGlobalH otKey(Keys.F6, MOD_SHIFT Or MOD_CONTROL)

    or

    UnregisterGloba lHotKey()

    --
    Newbie Coder
    (It's just a name)






    "Rotsey" <malcolm_smith@ RemoveThis.optu snet.com.auwrot e in message
    news:%238d$XT75 HHA.4880@TK2MSF TNGP03.phx.gbl. ..
    Hi,
    >
    I am writing app that I want to be able to hit a shortcut
    key (windows wide) and have it all launch my app and then
    be able to get the name of the process that was active when
    the key was pressed.
    >
    Is this possible and how?
    >
    I know I can assign a shortcut key to the app but how do
    I get the active process name?
    >
    rotsey
    >
    >

    Comment

    Working...