no i do not have relays hooked up to actual keys i have them hooked up to the caps,scroll and num lock lights. and if u new the first thing about relays spotty you would know that relays could not be hooked up to a key/button because relays alow current to pass through the circiut when a certain voltage is imputed, in this case the output to the led on the keyboard turns on the relay wich turns the light on. now that we know what a relay is i will tell you that i would not use the same computer i am using now to control my lights. i am using an old computer that is low power and ghz. and now you tell me how i am "breaking everything else" anyway enough with my harshness and how could you get a serial port to send signals to a relay or w/e. is there a tread on this. thx everyone...
You can p/invoke sendinput to send the key (VK_CAPITAL). (getkeystate can tell you if it is on or off) What is it the purpose If you want the user's input into your form to be in upper case or something like that, then there are better ways. (p/invoke = bad, managed code = good)
hello i tried doing some code with this and i am getting the "user32" showing up in red and none of the code i have found so far even can be a part of a "succsesful biuld". (i pasted some code from the sendput link) but what i am trying to do may sound weird but it is better than buying a 80$ phidget. i am trying to come up with a way to controll my lights in my house and having it all computerized. so i decided to take a part a keyboard and i added relays to the caps,scroll and num lock so i figured i could controll these from a program i make. thanks for the good reponse.
A quick question. So you have this program which will uses a modified keyboard and you've wired up realys to caps lock, scroll lock and num lock keys. Is there any reason you chose these keys as against F1, F2, F2 or A,B, C
So you are going to write this program that enables you to control these. I'm just curious as the keys are wired internal no different from the other keys (Correct ) and I am assuming that to control them you would require that you program is running (Correct ) and the state of these is maintained by windows and has significance to all programs running on this machine as well as you program.
It seems to me that all you really would need to do is to have the state maintained in you application and use of some form of Port rather than the keyboard would be a better route as this approach of trying to use the keyboard would effect all the programs in windows not just yours.
But, its your project and if you want to do it that way its up to you.
I know i dont know that much about VB but i still dont understand wuts rong, i think it wont import user32.dll, user32.dll is in red when i entered the code. could i be missing this .dll file i am using visual basic express edition and here is a list of errors i copied and pasted.
Error 1 Identifier expected. C:\Documents and Settings\Spencer\Local Settings\Application Data\Temporary Projects\WindowsApplication1\Form1.vb 15 68 WindowsApplication1 Error 3 Identifier expected. C:\Documents and Settings\Spencer\Local Settings\Application Data\Temporary Projects\WindowsApplication1\Form1.vb 29 52 WindowsApplication1 Error 4 Method 'Private Sub but1_Click(sender As Object)' cannot handle Event 'Public Event Click(sender As Object, e As System.EventArgs)' because they do not have the same signature. C:\Documents and Settings\Spencer\Local Settings\Application Data\Temporary Projects\WindowsApplication1\Form1.vb 29 104 WindowsApplication1 Error 2 Name 'key' is not declared. C:\Documents and Settings\Spencer\Local Settings\Application Data\Temporary Projects\WindowsApplication1\Form1.vb 19 37 WindowsApplication1 Error 5 Too many arguments to 'Private Sub DoKeyBoard(flags As NativeMethods.KEYEVENTF)'. C:\Documents and Settings\Spencer\Local Settings\Application Data\Temporary Projects\WindowsApplication1\Form1.vb 31 23 WindowsApplication1 Error 6 Too many arguments to 'Private Sub DoKeyBoard(flags As NativeMethods.KEYEVENTF)'. C:\Documents and Settings\Spencer\Local Settings\Application Data\Temporary Projects\WindowsApplication1\Form1.vb 32 51 WindowsApplication1
i am downloading user32.dll file now but i dont no where to put it, also i manually searched for user32.dll myself. do i have to include user32.dll as a referance.
Don't do anything with user32.dll it is part of windows, and you should have it already. It's in red as it is a string - it's not an error. Thinking...
hello i tried doing some code with this and i am getting the "user32" showing up in red and none of the code i have found so far even can be a part of a "succsesful biuld". (i pasted some code from the sendput link) but what i am trying to do may sound weird but it is better than buying a 80$ phidget. i am trying to come up with a way to controll my lights in my house and having it all computerized. so i decided to take a part a keyboard and i added relays to the caps,scroll and num lock so i figured i could controll these from a program i make. thanks for the good reponse
Number 2: You didnt make it clear that you were using the keyboard on a different machine to the one you were using. The lights on the keyboard change there status as a result of the action of pressing Scroll Lock/Num Lock or Caps Lock. This action has other effects in the system - ie Enabling Num Lock / Caps Lock functionality.
As you had'nt made it clear you were talking about running on another machine. But the scenario that it may cause problems is as follows:
If you were to try using your application to control the lights from your machine you were using and you were triggering it off this keyboard. And say you wanted to use other apps - lets say notepad. You start you app up and press caps lock to turn the lights on. This would then put this machine into caps lock mode and your lights are on - so you go to type in stuff into notepad whilst you app is still running - and it would be in caps lock mode. But I dont want to type in caps lock mode - so I press caps lock to turn it off and your lights would go off. The same would apply for the other keys as well.
So if you were using the machine that you app was running on for anything other than your application then using the lights on the keyboard of this machine would cause problems and you have a current limitation of 3 switches (Most keyboards have only 3 lights)
It seems rather a limited application to write an appliction that can control 3 lights and require a dedicate machine to run the process. It sounds as though you have a old spare machine but you may be going down an interesting but ultimately very limited scenario.
For what your trying to do I would definately be looking a X10 as a means of controlling devices such as lights etc. Very flexible and allows you to design solutions which can support many more and varied devices. Sure it requires some hardware purchase but its intentional home automation technology,
yes it is a deticated machine and i dont need to ever use notpad let alone on that machine. i would rather use this one and i'm not up on electronics lately.. wuts an x-10 (noob question) + could i make my own hardware that would work with the serial port......
To use it, you would create a new project, and add a button to form1. Copy the sample from the webpage (including the Imports statement). Delete all the code in Form1.vb and paste in the sample. Then it will run... Same should go for the SendInput sample.
As you are not sending Mouse or Hardware input, you can get rid of some code, to make it simpler. Here is an example which sends CapsLock. The Input structure has been simplified, and now only deals with keyboard input...
Imports System.Runtime.InteropServices
PublicClass Form1
PrivateWithEvents but1 AsNew Button
SubNew()
InitializeComponent()
but1 = New Button
but1.Location = New Point(10, 40)
but1.Text = "example"
Me.Controls.Add(but1)
EndSub
PrivateSub DoKeyBoard(ByVal flags As NativeMethods.KEYEVENTF, ByVal key As Keys)
' Do the something specified by flags with the key specified by key...
Dim input AsNew NativeMethods.INPUT
input.dwType = NativeMethods.InputType.Keyboard
input.wVk = Convert.ToInt16(key)
input.wScan = 0
input.time = 0
input.dwFlags = flags
input.dwExtraInfo = IntPtr.Zero
Dim cbSize AsInteger = Marshal.SizeOf(GetType(NativeMethods.INPUT))
Dim result AsInteger = NativeMethods.SendInput(1, input, cbSize)
If result = 0 Then Debug.WriteLine(Marshal.GetLastWin32Error)
EndSub
PrivateSub but1_Click(ByVal sender AsObject, ByVal e As System.EventArgs) Handles but1.Click
<DllImport("user32.dll", SetLastError:=True)> FriendSharedFunction SendInput(ByVal cInputs As Int32, ByRef pInputs As INPUT, ByVal cbSize As Int32) As Int32
Aha, it's the forum, it adds newlines to the code you copy.
In some places I have used the line continuation character _ at the end of a line. When you see one, delete any blank lines between it and the next statement, so it should look like this:
PrivateSub DoKeyBoard(ByVal flags As NativeMethods.KEYEVENTF, _ ByVal key As Keys)
not this:
PrivateSub DoKeyBoard(ByVal flags As NativeMethods.KEYEVENTF, _
ByVal key As Keys)
I'll edit the code above.
ok, it will work with just a paste now. I'd remove one blank line following each code line, they make it hard to read...
caps lock command
telek
no i do not have relays hooked up to actual keys i have them hooked up to the caps,scroll and num lock lights. and if u new the first thing about relays spotty you would know that relays could not be hooked up to a key/button because relays alow current to pass through the circiut when a certain voltage is imputed, in this case the output to the led on the keyboard turns on the relay wich turns the light on. now that we know what a relay is i will tell you that i would not use the same computer i am using now to control my lights. i am using an old computer that is low power and ghz. and now you tell me how i am "breaking everything else" anyway enough with my harshness and how could you get a serial port to send signals to a relay or w/e. is there a tread on this. thx everyone...
Average Joe
AnonymousALE
I went and got help from someone locally with this, they showed me how to do it. thank you for all of your reply's,
average joe
Tom Nguyen
What is it the purpose If you want the user's input into your form to be in upper case or something like that, then there are better ways. (p/invoke = bad, managed code = good)
JohnMcKenzie
rpa78
A quick question. So you have this program which will uses a modified keyboard and you've wired up realys to caps lock, scroll lock and num lock keys. Is there any reason you chose these keys as against F1, F2, F2 or A,B, C
So you are going to write this program that enables you to control these. I'm just curious as the keys are wired internal no different from the other keys (Correct ) and I am assuming that to control them you would require that you program is running (Correct ) and the state of these is maintained by windows and has significance to all programs running on this machine as well as you program.
It seems to me that all you really would need to do is to have the state maintained in you application and use of some form of Port rather than the keyboard would be a better route as this approach of trying to use the keyboard would effect all the programs in windows not just yours.
But, its your project and if you want to do it that way its up to you.
MadhuBabu
I know i dont know that much about VB but i still dont understand wuts rong, i think it wont import user32.dll, user32.dll is in red when i entered the code. could i be missing this .dll file i am using visual basic express edition and here is a list of errors i copied and pasted.
Error 1 Identifier expected. C:\Documents and Settings\Spencer\Local Settings\Application Data\Temporary Projects\WindowsApplication1\Form1.vb 15 68 WindowsApplication1
Error 3 Identifier expected. C:\Documents and Settings\Spencer\Local Settings\Application Data\Temporary Projects\WindowsApplication1\Form1.vb 29 52 WindowsApplication1
Error 4 Method 'Private Sub but1_Click(sender As Object)' cannot handle Event 'Public Event Click(sender As Object, e As System.EventArgs)' because they do not have the same signature. C:\Documents and Settings\Spencer\Local Settings\Application Data\Temporary Projects\WindowsApplication1\Form1.vb 29 104 WindowsApplication1
Error 2 Name 'key' is not declared. C:\Documents and Settings\Spencer\Local Settings\Application Data\Temporary Projects\WindowsApplication1\Form1.vb 19 37 WindowsApplication1
Error 5 Too many arguments to 'Private Sub DoKeyBoard(flags As NativeMethods.KEYEVENTF)'. C:\Documents and Settings\Spencer\Local Settings\Application Data\Temporary Projects\WindowsApplication1\Form1.vb 31 23 WindowsApplication1
Error 6 Too many arguments to 'Private Sub DoKeyBoard(flags As NativeMethods.KEYEVENTF)'. C:\Documents and Settings\Spencer\Local Settings\Application Data\Temporary Projects\WindowsApplication1\Form1.vb 32 51 WindowsApplication1
i am downloading user32.dll file now but i dont no where to put it, also i manually searched for user32.dll myself. do i have to include user32.dll as a referance.
Fuzzier
Thinking...
baptiste manson
It just sounds like a wau to make one thing work and break everything else.
BatchBoy
Number 1: I know what a relay is !!!
Number 2: You didnt make it clear that you were using the keyboard on a different machine to the one you were using. The lights on the keyboard change there status as a result of the action of pressing Scroll Lock/Num Lock or Caps Lock. This action has other effects in the system - ie Enabling Num Lock / Caps Lock functionality.
As you had'nt made it clear you were talking about running on another machine. But the scenario that it may cause problems is as follows:
If you were to try using your application to control the lights from your machine you were using and you were triggering it off this keyboard. And say you wanted to use other apps - lets say notepad. You start you app up and press caps lock to turn the lights on. This would then put this machine into caps lock mode and your lights are on - so you go to type in stuff into notepad whilst you app is still running - and it would be in caps lock mode. But I dont want to type in caps lock mode - so I press caps lock to turn it off and your lights would go off. The same would apply for the other keys as well.
So if you were using the machine that you app was running on for anything other than your application then using the lights on the keyboard of this machine would cause problems and you have a current limitation of 3 switches (Most keyboards have only 3 lights)
It seems rather a limited application to write an appliction that can control 3 lights and require a dedicate machine to run the process. It sounds as though you have a old spare machine but you may be going down an interesting but ultimately very limited scenario.
For what your trying to do I would definately be looking a X10 as a means of controlling devices such as lights etc. Very flexible and allows you to design solutions which can support many more and varied devices. Sure it requires some hardware purchase but its intentional home automation technology,
bj16060
yes it is a deticated machine and i dont need to ever use notpad let alone on that machine. i would rather use this one and i'm not up on electronics lately.. wuts an x-10 (noob question) + could i make my own hardware that would work with the serial port......
thanx for reply
average joe
Raj Amb
I put in a GetKeyboardState sample on pinvoke.net.
To use it, you would create a new project, and add a button to form1. Copy the sample from the webpage (including the Imports statement). Delete all the code in Form1.vb and paste in the sample. Then it will run...
Same should go for the SendInput sample.
As you are not sending Mouse or Hardware input, you can get rid of some code, to make it simpler. Here is an example which sends CapsLock. The Input structure has been simplified, and now only deals with keyboard input...
Imports System.Runtime.InteropServicesbut1 = New Buttonbut1.Location = New Point(10, 40)but1.Text = "example"Me.Controls.Add(but1)' Do the something specified by flags with the key specified by key...' simulate pressing caps lock, and releasing it:DoKeyBoard(0, Keys.CapsLock) ' 0 means KEYDOWN' the following line requires the statement:' Imports System.Runtime.Interopservices' at the top of the page...George Archer
But yeah, using a serial port would be better , and there is the IO.Ports.SerialPort class.
AndyWawa
In some places I have used the line continuation character _ at the end of a line. When you see one, delete any blank lines between it and the next statement, so it should look like this:
not this:
I'll edit the code above.
ok, it will work with just a paste now. I'd remove one blank line following each code line, they make it hard to read...
Jacob Lund
hi.
Is your problem solved
Thank you,
Bhanu.