Join 136,086 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,550 people online right now. Registration is fast and FREE... Join Now!
Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown If e.KeyCode = Keys.S Then Me.Visible = True'this part doesn't work' ElseIf e.KeyCode = Keys.H Then Me.Visible = False'this part works' End If End Sub End Class
how can i press a key and make the form visible or other thing if the form is not in the focus mode? the above code works only if the form is in focus mode how can i make it work if it is not in the focus mode or running in the form of a tray icon? i can it be something like the task manager ,i press ctrl +alt+del and it opens, how can i make something like that if it is running in the form of a tray icon can be visible by clicking on it or by pressing a combination of buttons?
and this code also desn't work
vb
Public Class Form1
Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown If e.KeyCode = Keys.S And e.KeyCode = Keys.D Then Me.Hide() End If End Sub End Class
it works if it was only one key pressed but more than one ley nothing happens any explanations or ideas or beter ways?
This post has been edited by narmer93: 24 Aug, 2008 - 01:45 PM
if the application can't be run if it is not in focus mode ,so how does the task manager work,or i have a prog called tuneup utilites ,there is a memory optimizer in it ,when i press ctrl+alt+o,it runs the optimizer in the tray icon how can i make something like that then?
and why doesn't multiple buttons work? as in the second code?
if the application can't be run if it is not in focus mode ,so how does the task manager work,or i have a prog called tuneup utilites ,there is a memory optimizer in it ,when i press ctrl+alt+o,it runs the optimizer in the tray icon how can i make something like that then?
and why doesn't multiple buttons work? as in the second code?
As far as i know the KeyDown event will only give you information regarding the last key that was pressed
I found this somewhere
QUOTE
The trouble that I can see with this is it seems that the KeyDown event is fired individually for each key; you'll either have to do use booleans as you indicated or the GetAsyncKeyState windows API function
CODE
Public Class Form1
Private Declare Function GetAsyncKeyState Lib "user32.dll" (ByVal keyCode As Integer) As Short
Private _txtBox As TextBox
Public Sub New()
' This call is required by the Windows Form Designer. InitializeComponent()
' Add any initialization after the InitializeComponent() call. UnimportantLayout()
End Sub
Private Sub TextOnKeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) _txtBox.AppendText(GetKeyString()) End Sub
Private Function GetKeyString() As String Dim sb As New System.Text.StringBuilder() Dim keys(4) As Short
If (WasKeyPressed(keys(0))) Then sb.Append("A, ") End If
If (WasKeyPressed(keys(1))) Then sb.Append("B, ") End If
If (WasKeyPressed(keys(2))) Then sb.Append("C, ") End If
If (WasKeyPressed(keys(3))) Then sb.Append("D, ") End If
If (sb.Length > 0) Then sb.Remove(sb.Length - 2, 2) End If
sb.AppendLine()
Return sb.ToString() End Function
Private Function WasKeyPressed(ByVal state As Short) As Boolean ' For some goofy reason, the compiler thinks that "&H8000" is not ' representable in Short, so I had to use an integer. Dim highBit As Integer = &H8000
Return ((state And highBit) = highBit) End Function
Private Enum VKey ' Find more key codes at ' http://msdn2.microsoft.com/en-us/library/ms645540.aspx A = &H41 B = &H42 C = &H43 D = &H44 E = &H45 End Enum
Private Sub UnimportantLayout() _txtBox = New TextBox() _txtBox.Multiline = True _txtBox.Margin = New Padding(10) _txtBox.Dock = DockStyle.Fill _txtBox.ReadOnly = True AddHandler _txtBox.KeyDown, AddressOf TextOnKeyDown Me.Controls.Add(_txtBox) End Sub
End Class
What I found with this program is that what keys can be detected (at least on my keyboard) is reliant upon the key's location. I could make no combination of C and D work, but A B C, and A B D were OK. You'll have to fill in the codes for more keys to see how it works out, but this would probably be easier than managing several globabl booleans.
This post has been edited by Damage: 24 Aug, 2008 - 09:50 PM
What you need is a hot key. Here is the code I use. If you have any questions just ask.
CODE
' Windows API declarations Private Const WM_HOTKEY As Integer = &H312 Public Const MOD_ALT As Integer = &H1 ' alt key down Public Const MOD_CONTROL As Integer = &H2 ' alt control down Public Const MOD_SHIFT As Integer = &H4 ' alt shift down
Public Declare Auto Function RegisterHotKey Lib "user32" (ByVal hWnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Boolean
Public Declare Auto Function UnregisterHotKey Lib "user32" (ByVal hWnd As IntPtr, ByVal id As Integer) As Boolean
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' you can combine alt, control and/or shift ' where I have 1 you can use any number but for each hotkey it must be unique so you can't have another 1 ' You can register more than one hot key 'RegisterHotKey(Me.Handle, 1, MOD_ALT Or MOD_CONTROL, Keys.Up) RegisterHotKey(Me.Handle, 1, MOD_ALT, Keys.M) RegisterHotKey(Me.Handle, 2, MOD_ALT Or MOD_CONTROL, Keys.Down) End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing UnregisterHotKey(Me.Handle, 1) End Sub
Protected Overrides Sub WndProc(ByRef message As System.Windows.Forms.Message) If (message.Msg = WM_HOTKEY) Then Dim hotKeyId As Integer = message.WParam.ToInt32()
If hotKeyId = 1 Then If Me.WindowState = FormWindowState.Minimized Then Me.WindowState = FormWindowState.Normal End If If hotKeyId = 2 Then If Me.WindowState <> FormWindowState.Minimized Then Me.WindowState = FormWindowState.Minimized End If End If MyBase.WndProc(message) '<<<<<< ALLWAYS ADD THIS = COMPUTER CRASH End Sub
is there a shorter way or easier code that can be used when the form is minimized or not in focus,and i press 2 or 3 keys and the form appears is there one to make it easier than the one u wrote?
is there a shorter way or easier code that can be used when the form is minimized or not in focus,and i press 2 or 3 keys and the form appears is there one to make it easier than the one u wrote?
Your never going to get anywhere in programming if you think that is too long. You should try making a program. There are lots of lines of code, but as programmers we enjoy writing it. But in this case you were just handed the code for free, and complain about it being too long. You should be more grateful and say thank you.
that is too long and i always say thanks when i get what i wanted and this code seems to be longer than to do this vb.net uses simple codes once i was trying to learn java and i was asking about how to make it play an mp3 file or an audio file and i got a long code while i can easily play .wav files in vb.net with a short line and this is the only part in the code that i understad
vb
If hotKeyId = 1 Then If Me.WindowState = FormWindowState.Minimized Then Me.WindowState = FormWindowState.Normal End If If hotKeyId = 2 Then If Me.WindowState <> FormWindowState.Minimized Then Me.WindowState = FormWindowState.Minimized End If End If
all the rest of the code is not easy to understand,at least not for me and currently i think i will learn C or C++ language and i am looking for the programming programme and i want a new version not a win95 version and before using it ,i have to be sure that it doesn't need anything like the .netframework or JRE and can work on normal windows is there something like that?
oh sorry cuz i didn't say thanks in the previous posts:)