keychanging program

keys are set as constants , is there anyway I can change them. or at least substitute them for the duration of my program

Comments

  • : keys are set as constants , is there anyway I can change them. or at least substitute them for the duration of my program
    :
    :

    If keys are set as constants, you will not be able to change them during program execution. Your best bet is to convert them to variables.

    If you want to protect the integrity of the data but be able to change, you can encapsulate the keys in a class or module and write property procedures that control access to the key value. Make the properties shared so that you dont have to instantiate the class.


    [code]
    Public Class Keys
    Private Shared mKeyA As String
    Private Shared mKeyB As String

    Public Shared Property KeyA As String
    Get
    Return mKeyA
    End Get
    Set(ByVal value As String)
    mKeyA = value
    End Set
    End Property

    Public Shared Property KeyB As String
    Get
    Return KeyB
    End Get
    Set(ByVal value As String)
    KeyB = value
    End Set
    End Property
    End Class
    [/code]

    Then you can access the keys like so ...

    [code]
    Dim key As String = Keys.KeyA
    keys.KeyB = "123456"
    [/code]

    The advantages of using property procedures lies in the fact thatyou can arbitrate extra conversions on the key before it gets stored. Inside the class u can write private (must be shared if manipulating shared data) functions that swap keys and whatever you need.

    If you are storing a ton of keys, consider using a hash table object.

    [code]
    Dim ht As New hashTable
    ht.Add("Key A", "123456")
    ht.Add("Key B", "2345")
    ht.Add("Key C", "345455")

    Dim keyA As String = ht("Key A").ToString

    [/code]

    I dunno if any of this helps. But from what minor info you gave I assume some of thgis will give some ideas.

  • I did find your bit greatly informative and will keep it handy for further use. But, correct me if I am wrong?. This is all obsolete as far as other applications are concerned. I really want to use the keys "Globally" so I can activate them from any application. I think I have to set a hook. Really, I want to know if I am on the right ack?
    I am delving a little deep for my level of programming. The api is not strait forward for a new programmer. I think I am close to getting it. I read a couple of articles. I will also get you back if I come across the solution so that you can better understand my problem. It also might be useful if someone else has the same trouble as I did.
    Thanks for your time always appreciated ...by tsikman
  • Ok I have figured out how to change my keys after months of headache.
    one thing I need to know now to complete my task. I am sending keys.
    I think I am sending them as text.
    for example sendkeys.send(" ") sends a space
    sendkeys.send(space) sends a 32
    I need the to get it to interact with the program
    since there is no text input field,
    Is there such thing as a space bar command in contrast to a space as a text input?
  • : Ok I have figured out how to change my keys after months of headache.
    : one thing I need to know now to complete my task. I am sending keys.
    : I think I am sending them as text.
    : for example sendkeys.send(" ") sends a space
    : sendkeys.send(space) sends a 32
    : I need the to get it to interact with the program
    : since there is no text input field,
    : Is there such thing as a space bar command in contrast to a space as a text input?
    :

    I was actually gonna mention about the SendKeys class, but wasn't sure what you were exactly doing. But try this:

    [code]

    SendKeys.Send("{SPACE}")

    [/code]

    Documentation suggest using these win32 api functions to set the focus on the application you want to send keys to:

    FindWindow
    SetForegroundWindow
  • [b][red]This message was edited by Tsikman at 2003-9-22 7:50:5[/red][/b][hr]
    you don't have to read this if you haven't time, but if it helps you to help me. I am all for it.(sorry I don't know how to grey out areas to make it easier to read).


    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


    Declare Sub keybd_event Lib "user32.dll" _
    (ByVal bVk As Byte, ByVal bScan As Byte, _
    ByVal KEYEVENTF_KEYUP As Long, ByVal dwExtraInfo As Long)



    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_X = &H58
    Public Const VK_SPACE = &H39
    Private Const WH_KEYBOARD_LL As Integer = 13&
    Public KeyboardHandle As Integer
    Const KEYEVENTF_KEYUP = &H2
    Const KEYEVENTF_EXTENDEDKEY = &H1

    ' 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_X) Then

    keybd_event(&H20, 0, KEYEVENTF_EXTENDEDKEY, 0)
    keybd_event(&H20, 0, KEYEVENTF_KEYUP, 0)
    Call HookedState("key was pressed")
    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

    _
    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



  • When you respond to a posting and want to to display the nifty grey code blocks just use ...

    [ code ]

    code goes here

    [ /code ]

    Accept there are no spaces inside the brackets.

    I will have to carefully scrutinize this code when I get a chance today. L8r.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories