|
-
Sep 3rd, 2013, 04:04 AM
#1
Thread Starter
New Member
Listview of global hotkeys
How can I code a listview where I will be able to register global hotkey's. When each global hotkey is presses the 3rd column's pass string should be copied to the clipboard.

This is the code i have so far.
Code:
Public Class Form1
<System.Runtime.InteropServices.DllImport("user32.dll")> Public Shared Function GetAsyncKeyState(ByVal vKey As System.Windows.Forms.Keys) As Short
End Function
Dim sks As String = "8-BckSpc;9-Tab;13-Enter;16-Shft;17-Ctrl;18-Alt;33-PgUp;34-PgDn;35-End;36-Home;37-Left;38-Up;39-Right;40-Down;45-Ins;46-Del;112-F1;113-F2;114-F3;115-F4;116-F5;117-F6;118-F7;119-F8;120-F9;121-F10;122-F11;123-F12;"
Dim fcombo(3), combo(3), fcnt, kcnt As Integer
Dim nochk As Boolean = False
Dim blnAdd As Boolean 'indicator if add button was pressed
Dim addcount As Integer = 0
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Interval = 100
Timer1.Start()
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
addcount = addcount + 1
If addcount >= 2 Then
For x = 0 To 3
combo(x) = 0
Next
kcnt = 0
End If
If btnAdd.Text.ToLower() = "add" Then 'if caption is ADD then perform Add process
txtName.Enabled = True
txtHk.Enabled = True
txtPass.Enabled = True
btnAdd.Text = "Save"
btnEdit.Text = "Cancel"
btnDelete.Enabled = False
txtName.Text = ""
txtHk.Text = ""
txtPass.Text = ""
blnAdd = True
Else 'save process
txtName.Enabled = False
txtHk.Enabled = False
txtPass.Enabled = False
btnAdd.Text = "Add"
btnEdit.Text = "Edit"
btnDelete.Enabled = True
If blnAdd = True Then
AddItemToListView()
Else
EditItemInListView()
End If
End If
End Sub
Private Sub AddItemToListView()
'Usually the first unique colum is the root item
Dim lv As BetterListViewItem = ListView1.Items.Add(txtName.Text)
'The remaining columns are subitems
lv.SubItems.Add(txtHk.Text)
lv.SubItems.Add(txtPass.Text)
End Sub
Private Sub EditItemInListView()
If ListView1.SelectedItems.Count > 0 Then 'make sure there is a selected item to modify
ListView1.SelectedItems(0).Text = txtName.Text
ListView1.SelectedItems(0).SubItems(1).Text = txtHk.Text
ListView1.SelectedItems(0).SubItems(2).Text = txtPass.Text
End If
End Sub
Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
If txtName.Text.Length > 0 Then
If btnEdit.Text.ToLower() = "edit" Then 'if caption is EDIT then perform EDIT process
txtName.Enabled = True
txtHk.Enabled = True
txtPass.Enabled = True
btnAdd.Text = "Save"
btnEdit.Text = "Cancel"
btnDelete.Enabled = False
blnAdd = False
Else 'cancel process
txtName.Enabled = False
txtHk.Enabled = False
txtPass.Enabled = True
btnAdd.Text = "Add"
btnEdit.Text = "Edit"
btnDelete.Enabled = True
End If
Else
MessageBox.Show("Please select record to edit")
End If
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
If ListView1.SelectedItems.Count > 0 AndAlso MessageBox.Show("Do you want to delete this item?", "Confirm", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then 'make sure there is a selected item to delete
ListView1.SelectedItems(0).Remove()
End If
End Sub
Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
If ListView1.SelectedItems.Count > 0 Then
txtName.Text = ListView1.SelectedItems(0).Text
txtHk.Text = ListView1.SelectedItems(0).SubItems(1).Text
txtPass.Text = ListView1.SelectedItems(0).SubItems(2).Text
End If
End Sub
Private Sub txtHk_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtHk.KeyDown
If Not combo.Contains(e.KeyValue) And kcnt < 4 Then
If kcnt > 0 Then txtHk.Text &= " + "
Dim fn As Integer = sks.IndexOf(e.KeyValue.ToString & "-")
If fn > -1 Then
Dim keystr As String = sks.Substring(fn + 1 + e.KeyValue.ToString.Length)
keystr = keystr.Remove(keystr.IndexOf(";"))
txtHk.Text &= keystr
Else
txtHk.Text &= Chr(e.KeyValue)
End If
combo(kcnt) = e.KeyValue
kcnt += 1
End If
End Sub
Private Sub txtHk_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtHk.KeyUp
nochk = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If txtHk.Focused = False Then
Dim x As Integer = 8
Dim kdwn As Boolean = False
For x = 8 To 255
If GetAsyncKeyState(CType(x, Keys)) <> 0 Then
kdwn = True
If Not fcombo.Contains(x) And fcnt < 4 Then
fcombo(fcnt) = x
fcnt += 1
If nochk = False And kcnt = fcnt And combo.Contains(fcombo(0)) And combo.Contains(fcombo(1)) And combo.Contains(fcombo(2)) And combo.Contains(fcombo(3)) Then
MessageBox.Show("Key Combo Pressed")
End If
End If
End If
If x = 159 Then x = 165
Next
If kdwn = False And fcnt > 0 Then
fcnt = 0
nochk = False
For c = 0 To 3
fcombo(c) = 0
Next
End If
End If
End Sub
End Class
The changes I need to make are :-
------ Use global hotkey register - But I think I wont be able to use more than one normal character in the combination like( Ctrl + A + S).
------ Iam still not able to check for the hotkey combination in the listview. The correct function should be. If hotkey present in column 2 then do copy pass string from column 3.
Any help would be appreciated.
-
Sep 3rd, 2013, 07:57 AM
#2
Re: Listview of global hotkeys
I don't think GetAsyncKeyState is the appropriate API to use here. A global hot key is in the definition of the word, global. So even if you manage to get them, it will still do what it is supposed to do. You should actually bypass those keys if you want to keep track of them.
By bypassing the hotkeys I mean that you have to intercept them ( so that they do not do what they're programmed to do ). I'd suggest using the RegisterHotkey and UnregisterHotKey API with this list of constants :
http://msdn.microsoft.com/en-us/library/ms927178.aspx
in your program. Just my opinion
-
Sep 3rd, 2013, 01:29 PM
#3
Re: Listview of global hotkeys
Ditch the ListView for a DatagridView. Then throw out all the code you have above and follow Hannes' suggestion!
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|