|
-
Dec 15th, 2011, 06:24 PM
#1
Thread Starter
Junior Member
[RESOLVED] Creating Patient Event Logger for a Hospital NICU
Hi guys! I'm new here and this is my first post. 
I am currently working on a way to create a keystroke logger program of sorts, to be used in a hospital neonatal intensive care unit. It will be used in a project to look for ways to identify problems shortly after birth in newborn infants. (I was approached to create this program as I am the only one here with any programming experience, and I kinda put my foot in my mouth and said yes! D'oh!) While I have dabbled with C programming about 15 years ago (in DOS) and have some current familiarity with Excel-based VBA scripting, I have absolutely no experience with coding from scratch in Visual Basic (I'm a neurobiologist, not a programmer).
This program (I am using VB 2010 Express) is going to be used to record user-defined time-stamped events (such as "Movement", "Cough", "Cry"). A keyboard attached by the newborn's bassinet will be connected to a laptop which runs the program. The reason for time-stamping (in seconds) is so the events can be synced with video and other physiological recordings.
An example of a very simplified file would be something like this (an actual file would be longer, of course)
0 start
90 cough
260 movement
510 cry
785 stop
Right now, using sample code on the net, I've gotten a program that reads keyboard entries directly into a textbox on the main program.
Code:
Public Declare Function GetAsyncKeyState Lib "user32.dll" (ByVal vKey As Int32) As UShort
Private Sub Timer1_Tick() Handles Timer1.Tick
Dim keyResult As Integer
Dim chrKey As String
Dim i As Integer
For i = 2 To 90
keyResult = 0
keyResult = GetAsyncKeyState(i)
If keyResult = -32767 Then
chrKey = Chr(i)
MsgBox(chrKey)
Exit For
End If
Next i
End Sub
I understand how this code works, but can't seem to find anything on getting time-stamped data into it, and displaying each entered character on a new line. Also, currently, all keystrokes are recorded so backspace deletes stuff, which obviously is not good.
Things I will have to add:
1. Allow only specific keystrokes to be recorded.
2. Each keystroke should be recorded as a phrase (for example, pressing Q might be recorded as "movement". W might be "cough". E might be "cry". etc etc.)
3. Display of each event preceded by a time stamp (in seconds) from when the "Start" button was clicked.
4. "Start" button has to switch to display "Stop" once recording begins, with pressing "Stop" ending the recording. "Stop" button switches to display "Start" once recording is stopped.
5. All events (and time-stamps) will have to be written into a plain text file (i.e. eventfile1.log) and saved somewhere. I have no idea if this can be done during recording, or would have to be done at the end after hitting "Stop".
6. Pressing "Start" again clears everything and begins recording in a new file.
I have been looking up sample code online, and I realise this program is similar in a way to a keylogger. However, typing "keylogger" into google brings up some websites I'm not sure I ought to be visiting! Does anyone know of any safe sites with good sample code that might be useful? Is everything that I hope to accomplish even possible in Visual Basic? (I hope so!)
If anyone has any pointers on how to go about writing this program, they would be most appreciated! Thank you!
-
Dec 16th, 2011, 05:55 AM
#2
Member
Re: Creating Patient Event Logger for a Hospital NICU
http://www.vbforums.com/forumdisplay.php?f=13
It sounds like a school project you want to get done wihtout lifting a finger. If is a real project post it on the projects forum.
-
Dec 16th, 2011, 06:13 AM
#3
Re: Creating Patient Event Logger for a Hospital NICU
Keylogging a machine is as you have found out, not a good idea (and frowned upon on this forum). However, there is nothing stopping you from just using the keypress events from a form (see here), as your form will be active, it will be possible. If however this program will be run in the background and another program will have focus, ie a patient record, then keylogging is still a bad idea. While trying to type into said patient record your application would be constantly recording actions.
Is there any reason why a set of buttons can not be used?
-
Dec 16th, 2011, 06:16 AM
#4
Re: Creating Patient Event Logger for a Hospital NICU
There is a difference between a keylogger and hotkeys, A keylogger just records all keystrokes to a file, whilst a hotkey run's code when the key is pressed. Check out the hotkey handler class in my signature, although if you are a beginner I doubt you will be able to understand how the code works.
-
Dec 16th, 2011, 06:20 AM
#5
Re: Creating Patient Event Logger for a Hospital NICU
Instead of "keylogger" search for "hotkey" - you can set up hotkeys for the different events and have them fire when the nurse presses them.
-
Dec 16th, 2011, 07:45 AM
#6
Thread Starter
Junior Member
Re: Creating Patient Event Logger for a Hospital NICU
 Originally Posted by yo mismo
Hi, Yo Mismo. I am not looking for someone to create the program for me. I am looking for info on where I can obtain resources to learn how to code this. I think it's important that I code it myself so I know how it works. Otherwise, how am I going to modify it in the future? 
 Originally Posted by Grimfort
Keylogging a machine is as you have found out, not a good idea (and frowned upon on this forum). However, there is nothing stopping you from just using the keypress events from a form ( see here), as your form will be active, it will be possible. If however this program will be run in the background and another program will have focus, ie a patient record, then keylogging is still a bad idea. While trying to type into said patient record your application would be constantly recording actions.
Is there any reason why a set of buttons can not be used?
Hi Grimfort. Yes, the program will be active. It should ONLy be recording keypresses when the program is in focus, and everything it records should be clearly displayed on screen. You are right that "keylogger" is probably not the right description for what I have in mind. Thank you for the link to the keypress from events example.
The reason why only selected buttons should work is because there are about 20 events that have to be assigned to specific keys. (e.g., pressing W would be recorded as "movement"), and all other keys should not create any entries in the log file.
 Originally Posted by BlindSniper
There is a difference between a keylogger and hotkeys, A keylogger just records all keystrokes to a file, whilst a hotkey run's code when the key is pressed. Check out the hotkey handler class in my signature, although if you are a beginner I doubt you will be able to understand how the code works.
Hi BlindSniper. You are right. Hotkey is definitely a more appropriate term. Thank you for pointing me to the hotkey handler class. I will have a look at it and see if I can figure it out.
 Originally Posted by Merrion
Instead of "keylogger" search for "hotkey" - you can set up hotkeys for the different events and have them fire when the nurse presses them.
Hi Merrion. Thank you for the advice. I will do that!
-
Dec 16th, 2011, 08:26 AM
#7
Re: Creating Patient Event Logger for a Hospital NICU
 Originally Posted by Mudcake
Hi Grimfort. Yes, the program will be active. It should ONLy be recording keypresses when the program is in focus, and everything it records should be clearly displayed on screen.
Then the keypreview and form keypress is what you need to do. The hotkeys will work even when the application does not have focus.
By buttons, I mean actual button controls on the form instead of keyboard buttons .
ie
[Start]
[Cough] [Sleep] [Movement] [Cry]
[Stop]
It will stop users having to remember which key is which .
-
Dec 16th, 2011, 08:39 AM
#8
Re: Creating Patient Event Logger for a Hospital NICU
And then you can easily have the buttons hot keyed.
Say your button text is "Start". If you set it to "&Start" then if you press Alt S it will be like you clicking on the button.
-
Dec 16th, 2011, 09:47 AM
#9
Hyperactive Member
Re: Creating Patient Event Logger for a Hospital NICU
 Originally Posted by yo mismo
As the OP has included both an attempt and theory behind what is required from his project, helping (in my opinion) should not be hindered by the "not doing your homework" stigma.
Anyway... Hi Mudcake welcome to VBForums 
What you're after really isn't as complex as you may think, essentially you need to monitor IF specific keys are pressed and THEN if they are what is the output.
Therefore as others have previously stated keylogging isn't what you're after, what you're after is capturing a keydown event and assigning it to an output.
Okay to start off you're going to need to decide what keys are assigned to what functionality.
For now I'll follow the below..
pressing Q might be recorded as "movement". W might be "cough". E might be "cry".
If you go to your project and your form view, place a textbox from the toolbox onto the form. By default it will be named TextBox1.
We'll use this textbox as the place you enter the 'input'.
In the code, you need a sub which will monitor keydown events in that textbox, you can create this by going to your code and at the top, in the left hand drop down, select textbox1. Then go to the right hand drop down and select the KeyDown event. This will generate the below for you.
vbnet Code:
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles TextBox1.KeyDown
End Sub
And then within that sub include the below (per key you wish to control)
vbnet Code:
If e.KeyData = Keys.W Then
MsgBox("Cough")
End If
If you want more information on the KeyDown event or other handling keyboard events in general, I just discovered a member of VBForums (jmcilhinney) has written a blog on the subject here which should prove to be useful for future learning.
Last edited by OhGreen; Dec 16th, 2011 at 10:02 AM.
-
Dec 16th, 2011, 09:53 AM
#10
Hyperactive Member
Re: Creating Patient Event Logger for a Hospital NICU
Following on from that, dependant on how you want to 'log' your output. You could perhaps place "cough" or whatever on a new line in a text file (rather than my example of making a msgbox popup which isn't practical for what you're after, but shows it's working!).
Googling how to write to a .txt file from vb.net using a streamwriter would point you in the right direction to do just that.
Last edited by OhGreen; Dec 16th, 2011 at 10:49 AM.
Reason: Corrected - Thanks BlindSniper.
-
Dec 16th, 2011, 10:13 AM
#11
Re: Creating Patient Event Logger for a Hospital NICU
 Originally Posted by OhGreen
Googling how to write to a .txt file from vb.net using a streamreader would point you in the right direction to do just that.
You mean streamwriter don't you?
-
Dec 16th, 2011, 10:31 AM
#12
Re: Creating Patient Event Logger for a Hospital NICU
Last edited by BlindSniper; Dec 16th, 2011 at 11:18 AM.
-
Dec 16th, 2011, 10:48 AM
#13
Hyperactive Member
Re: Creating Patient Event Logger for a Hospital NICU
I do, after a week at work and the prospect of Christmas break for 10 days my brain is in Friday afternoon mode.
-
Dec 16th, 2011, 11:26 AM
#14
Re: Creating Patient Event Logger for a Hospital NICU
Here's my thoughts.
Add a Menu Strip with your Start/Stop and Event options. As someone above pointed out you can attach shortcuts to these menu items.
Create a typed data set that will store patient, event, time stamp information.
To add an event you would a row to your data table.
Add a grid to your form, and bind the data set to it. This will display your input.
Add methods to export/import the dataset's xml so you can save the information.
At some point you may want to look at using a central database (rather than XML) that all the NICU applications connect to.
This may look like a lot of work, but tackle it one piece at a time, and post back with any questions.
That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma
Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney
-
Dec 16th, 2011, 11:33 AM
#15
Hyperactive Member
Re: Creating Patient Event Logger for a Hospital NICU
Just thought, no one has addressed the "Time Stamped" issue.
This can be achieved simply by adding the value TimeOfDay which takes the time from your system clock (as shown below):
vbnet Code:
If e.KeyData = Keys.W Then MsgBox(TimeOfDay & " " & "Cough") End If
-
Dec 16th, 2011, 12:07 PM
#16
Thread Starter
Junior Member
Re: Creating Patient Event Logger for a Hospital NICU
 Originally Posted by Grimfort
By buttons, I mean actual button controls on the form instead of keyboard buttons  .
Grimfort, we will be using an external keyboard connected to a laptop. Each key on the keyboard will be labelled with the appropriate event type. For timing accuracy reasons (and timing is important if we wish to sync the events with other recordings like EEG and EKG), I suspect hitting a key on a keyboard would be quicker than grabbing the mouse (or using the touchpad), moving over a button, and clicking. Also, with about 25 event types, that's a lot of buttons.
 Originally Posted by OhGreen
Anyway... Hi Mudcake welcome to VBForums
What you're after really isn't as complex as you may think, essentially you need to monitor IF specific keys are pressed and THEN if they are what is the output.
Therefore as others have previously stated keylogging isn't what you're after, what you're after is capturing a keydown event and assigning it to an output.
Thank you for the welcome, OhGreen.
I have implemented your suggestion and it works great. However, the input repeats if the key is held down. Is there any way to prevent that?
This is what I've come up with so far. I used SuppressKeyPress to prevent the actual keystroke appearing in the textbox as well (so I don't get "movementq".
vbnet Code:
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyData = Keys.Q Then ' If Keypress is Q
e.SuppressKeyPress = True ' Prevents actual keypress appearing
TextBox1.AppendText("Movement" & vbNewLine) ' Inputs event type and hit return
ElseIf e.KeyData = Keys.W Then
e.SuppressKeyPress = True
TextBox1.AppendText("Cough" & vbNewLine)
ElseIf e.KeyData = Keys.E Then
e.SuppressKeyPress = True
TextBox1.AppendText("Cry" & vbNewLine)
End If
End Sub
Unfortunately, I haven't figured out how to disable unwanted key inputs such as "," or "." or Delete, Backspace, Enter, etc. To accomplish this, would I have to include an ElseIf statement for every single possible keypress with a "e.SuppressKeyPress = True"? It seems like a rather inelegant method.
Thank you too for pointing me to Steamwriter (and to BlindSniper for the correct name). I am sure I will get to that part of the project eventually, but I am concentrating on getting the right input into the textbox for now. I got the Start/Stop button working with:
vbnet Code:
Private Sub BtnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStart.Click
If Timer1.Enabled = False Then ' If the Timer isn't running
Timer1.Enabled = True ' Turn the Timer on
BtnStart.Text = "Stop" ' Button text now says Stop
TextBox1.Focus() ' Restore focus to textbox after button press
Else ' If Timer is running
Timer1.Enabled = False ' Turn the Timer off
BtnStart.Text = "Start" ' Button text reverts to Start
TextBox1.Focus() ' Restores focus to textbox after button press
End If
End Sub
I just have to figure out how to get that button to start the count up timer (which I have now learned is called a Stopwatch), display the elapsed time on Label1, and append the elapsed time in front of every event type in the TextBox! 
Wild Bill, this is my expression when I read your post ---> 
Waaay over my head, but I will try to digest it slowly! Thank you though. The typed data set idea sounds interesting, but I have no idea what that is. Using a central database is, unfortunately (or fortunately, from my point of view!), not an option for us. While we have access to database servers here in New York, this program may eventually have to be used in several rural hospitals around the country where such infrastructure is not available. Our plan is to keep the program as simple as possible, as it will have to be used by some staff who are not very familiar with computers.
-
Dec 16th, 2011, 02:37 PM
#17
Thread Starter
Junior Member
Re: Creating Patient Event Logger for a Hospital NICU
 Originally Posted by OhGreen
Just thought, no one has addressed the "Time Stamped" issue.
This can be achieved simply by adding the value TimeOfDay which takes the time from your system clock (as shown below):
Thanks, OhGreen. Thing is, I am looking to display elapsed time, rather than time of day. But I managed to figure out how to do that thanks to online tutorials. The last time I coded a program from scratch was in 96 on the Borland C compiler (for DOS). Visual Studio is definitely a vast improvement over that! And the massive amounts of sample code and tutorials online nowadays really help. All I had back in those days was the help file and a library book or two.
I've managed to get a couple more things working. The counter now displays and counts properly, and is controlled by the Start/Stop button. I had a hell of a time trying to get the timer disabled on load, but finally figured out I had to use EventLogger_Load1 instead of Form_Load. Writing to file is now also working, using StreamWriter. I also figured out how to handle invalid keystrokes without having an IfElse statement for every single key on the keyboard (which would have been a pain and so so clunky).
Edit: Just realised I can "Else e.SuppressKeyPress = True" to handle all the invalid keystrokes entries. Silly me! 
vbnet Code:
Public Class EventLogger
' This program is designed to read keyboard inputs and output the corresponding patient events to a plain text file
' Development commenced: December 15 2011
Dim StopWatch As New Diagnostics.Stopwatch
Dim EventFile As System.IO.StreamWriter
Private Sub EventLogger_Load1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Timer1.Enabled = False ' Runs on load and disables Timer till Start is clicked
Label1.Text = "0" ' Sets Stopwatch counter on screen to 0 at the Start
EventFile = My.Computer.FileSystem.OpenTextFileWriter("C:\Eventuality\PatientX.states", True)
End Sub
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyData >= Keys.A And e.KeyData <= Keys.Z Then
If e.KeyData = Keys.Q Then ' If Keypress is Q
EventFile.WriteLine(Label1.Text & vbTab & "Movement") ' Writes event type to text file
e.SuppressKeyPress = True ' Prevents actual keypress appearing
TextBox1.AppendText(Label1.Text & vbTab & "Movement" & vbNewLine) ' Inputs event type and hit return
ElseIf e.KeyData = Keys.W Then
EventFile.WriteLine(Label1.Text & vbTab & "Cough")
e.SuppressKeyPress = True
TextBox1.AppendText(Label1.Text & vbTab & "Cough" & vbNewLine)
ElseIf e.KeyData = Keys.E Then
EventFile.WriteLine(Label1.Text & vbTab & "Cry")
e.SuppressKeyPress = True
TextBox1.AppendText(Label1.Text & vbTab & "Cry" & vbNewLine)
Else
e.SuppressKeyPress = True
End If
Else
e.SuppressKeyPress = True
End If
End Sub
Private Sub BtnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStart.Click
If Timer1.Enabled = False Then ' If the Timer isn't running
Timer1.Enabled = True ' Turn the Timer on
Me.StopWatch.Start() ' Inits Stopwatch
EventFile.WriteLine(Label1.Text & vbTab & "Start Acquisition") ' Writes event type to text file
TextBox1.AppendText(Label1.Text & vbTab & "Start Acquisition" & vbNewLine) ' Inputs event type and hit return
BtnStart.Text = "Stop" ' Button text now says Stop
TextBox1.Focus() ' Restore focus to textbox after button press
Else ' If Timer is running
Me.StopWatch.Reset() ' Resets Stopwatch
Timer1.Enabled = False ' Turn the Timer off
EventFile.WriteLine(Label1.Text & vbTab & "Stop Acquisition") ' Writes event type to text file
TextBox1.AppendText(Label1.Text & vbTab & "Stop Acquisition" & vbNewLine) ' Inputs event type and hit return
Label1.Text = "0" ' Resets Stopwatch counter on screen
BtnStart.Text = "Start" ' Button text reverts to Start
EventFile.Close() ' Closes Text File
TextBox1.Focus() ' Restores focus to textbox after button press
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim elapsed As TimeSpan = Me.StopWatch.Elapsed ' I don't quite know what this line does but hey, it works!
Label1.Text = String.Format("{0}", Math.Floor((elapsed.Minutes * 60) + elapsed.Seconds)) ' This formats the text string in label to display elapsed time in seconds
End Sub
End Class
Left on my to-do list is:
1. Figuring out how to stop repeated entries when a key is held down.
2. Asking user to enter a filename for the text file when the program starts up.
Last edited by Mudcake; Dec 16th, 2011 at 04:13 PM.
-
Dec 19th, 2011, 11:01 AM
#18
Re: Creating Patient Event Logger for a Hospital NICU
Code:
'This
Dim elapsed As TimeSpan = Me.StopWatch.Elapsed
Label1.Text = String.Format("{0}", Math.Floor((elapsed.Minutes * 60) + elapsed.Seconds))
'Can be shortend to this
Label1.Text = Me.StopWatch.Elapsed.TotalSeconds.ToString
That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma
Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney
-
Dec 19th, 2011, 11:10 AM
#19
Thread Starter
Junior Member
Re: Creating Patient Event Logger for a Hospital NICU
 Originally Posted by wild_bill
Code:
'This
Dim elapsed As TimeSpan = Me.StopWatch.Elapsed
Label1.Text = String.Format("{0}", Math.Floor((elapsed.Minutes * 60) + elapsed.Seconds))
'Can be shortend to this
Label1.Text = Me.StopWatch.Elapsed.TotalSeconds.ToString
Thank you, Wild Bill. However, using that causes seconds to be displayed to 7 decimal places. Is there any way to prevent that? I just want seconds displayed as whole numbers.
-
Dec 19th, 2011, 11:25 AM
#20
Hyperactive Member
Re: Creating Patient Event Logger for a Hospital NICU
Effectively if you hold a key down, you're telling the computer to keep inputting.
If you want the user to be able to hold the key down... when is the trigger for the "cough"; when the key is pressed or when the user eventually lifts their finger off the key?
-
Dec 19th, 2011, 11:29 AM
#21
Thread Starter
Junior Member
Re: Creating Patient Event Logger for a Hospital NICU
 Originally Posted by OhGreen
Effectively if you hold a key down, you're telling the computer to keep inputting.
If you want the user to be able to hold the key down... when is the trigger for the "cough"; when the key is pressed or when the user eventually lifts their finger off the key?
OhGreen, the trigger for the event should be on keydown. I tried changing KeyDown to KeyPress (I'm not sure if that is the right approach), but I got errors.
-
Dec 19th, 2011, 11:31 AM
#22
Re: Creating Patient Event Logger for a Hospital NICU
 Originally Posted by Mudcake
Thank you, Wild Bill. However, using that causes seconds to be displayed to 7 decimal places. Is there any way to prevent that? I just want seconds displayed as whole numbers.
Code:
Label1.Text = Me.StopWatch.Elapsed.TotalSeconds.ToString("0")
That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma
Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney
-
Dec 19th, 2011, 11:40 AM
#23
Thread Starter
Junior Member
Re: Creating Patient Event Logger for a Hospital NICU
 Originally Posted by wild_bill
Code:
Label1.Text = Me.StopWatch.Elapsed.TotalSeconds.ToString("0")
Thank you, Wild Bill. That's perfect!
-
Dec 19th, 2011, 12:01 PM
#24
Hyperactive Member
Re: Creating Patient Event Logger for a Hospital NICU
You can't use KeyPress as that only handles non-character key's such as Enter.
You'd have to do something similar to the below:
vbnet Code:
Dim keypressed As Boolean Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles TextBox1.KeyDown If keypressed = False Then If e.KeyData = Keys.W Then MsgBox(TimeOfDay & " " & "Cough") keypressed = True End If End If End Sub Private Sub TextBox1_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp If e.KeyData = Keys.W Then keypressed = False End If End Sub
-
Dec 19th, 2011, 12:04 PM
#25
Thread Starter
Junior Member
Re: Creating Patient Event Logger for a Hospital NICU
 Originally Posted by OhGreen
You can't use KeyPress as that only handles non-character key's such as Enter.
You'd have to do something similar to the below:
Thanks for the explanation and the sample code, OhGreen!
-
Dec 19th, 2011, 05:11 PM
#26
Re: Creating Patient Event Logger for a Hospital NICU
1. Figuring out how to stop repeated entries when a key is held down.
Rather than using a textbox/key press event, you could handle the form's key up event.
Code:
Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
Debug.WriteLine(e.KeyData)
End Sub
That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma
Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney
-
Dec 19th, 2011, 06:24 PM
#27
Thread Starter
Junior Member
Re: Creating Patient Event Logger for a Hospital NICU
Wild Bill, for accuracy reasons, I would prefer to use the keydown rather than keyup, but thank you for that suggestion.
OhGreen, I ultimately went with a variation of your method. Your code was really helpful and helped me understand what needed to be done. Thank you.
By moving the KeyPressed = True out of the nested If loop, I could get away with using just one line, rather than having to copy/paste it into the loop for every single valid keystroke.
I also figured that by removing the If loop from keyup, I could use the same subroutine for all keyups, since no matter which key was being released, I would wish KeyPressed to be set back to False anyway.
That just reduces the complexity of the code, I think (and makes it more elegant). Hopefully, I have not missed any glaring logic errors by doing so. The program seems to run fine when I tested it. Fingers crossed!
Kinda exciting that my very first program is finally nearing completion! Couldn't have done it without the help of those of you who contributed to this thread!
vbnet Code:
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles TextBox1.KeyDown
If KeyPressed = False Then
KeyPressed = True
If e.KeyData = Keys.D1 Then ' If Keypress is 1
e.SuppressKeyPress = True ' Prevents actual keypress appearing
EventFile.WriteLine(Label1.Text & vbTab & "Sigh") ' Writes event type to text file
TextBox1.AppendText(Label1.Text & vbTab & "Sigh" & vbNewLine) ' Inputs event type and hit return
Else
e.SuppressKeyPress = True ' Supresses all invalid keystrokes
End If
Else
e.SuppressKeyPress = True ' Supresses all invalid keystrokes
End If
End Sub
Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
KeyPressed = False
End Sub
-
Dec 20th, 2011, 11:59 AM
#28
Thread Starter
Junior Member
Re: Creating Patient Event Logger for a Hospital NICU
I am posting here the complete code for my program (though I cut out most of the huge list of different event types to reduce clutter), in hopes that it might be useful in some way to other newbies in the future.
I could not have got this program written without the help of those who contributed to this thread, so a very big thank you to each of you! 
vbnet Code:
Public Class EventLogger
' Eventuality - Version 1.0
' This program is designed to read keyboard inputs and output the corresponding patient events to a plain text file
' Development commenced: December 15 2011
' Program Release: December 19 2011
Dim StopWatch As New Diagnostics.Stopwatch
Dim EventFile As System.IO.StreamWriter
Dim KeyPressed As Boolean = False
Private Sub EventLogger_Load1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Timer1.Enabled = False ' Runs on load and disables Timer till Start is clicked
Label1.Text = "0" ' Sets Stopwatch counter on screen to 0 at the Start
TextBox1.Text = vbNewLine & vbNewLine & vbNewLine & vbNewLine & vbNewLine & vbNewLine & vbNewLine & _
" Eventuality Ver: 1.0"
TextBox1.Enabled = False ' Temporarily disables input to Textbox
If My.Computer.FileSystem.DirectoryExists("C:\Eventuality\") = False Then ' Checks if Eventuality directory does not exist
My.Computer.FileSystem.CreateDirectory("C:\Eventuality\") ' If it does not, create it
End If
SaveFileDialog1.InitialDirectory = "C:\Eventuality\" ' Sets default directory
SaveFileDialog1.DefaultExt = "states" ' Sets default file extension
If SaveFileDialog1.ShowDialog() = DialogResult.OK Then ' Get user defined filename and create file
Dim NameString As String = System.IO.Path.GetFileNameWithoutExtension(SaveFileDialog1.FileName)
EventFile = My.Computer.FileSystem.OpenTextFileWriter("C:\Eventuality\" & NameString & ".states", True)
Else
End ' If user hits Cancel or does not enter a filename, program terminates
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Label1.Text = Me.StopWatch.Elapsed.TotalSeconds.ToString("0") ' Formats the text string in Label to display the elapsed time in seconds
End Sub
Private Sub BtnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStart.Click
If Timer1.Enabled = False Then ' If the Timer isn't running
TextBox1.Enabled = True ' Enables Textbox for input
TextBox1.Text = "" ' Resets Textbox contents
Timer1.Enabled = True ' Turn the Timer on
Me.StopWatch.Start() ' Inits Stopwatch
EventFile.WriteLine(Label1.Text & vbTab & "Start Acquisition") ' Writes event type to text file
TextBox1.AppendText(Label1.Text & vbTab & "Start Acquisition" & vbNewLine) ' Inputs event type and hit return
BtnStart.Text = "Stop" ' Button text now says Stop
TextBox1.Focus() ' Restore focus to textbox after button press
Else ' If Timer is running
Me.StopWatch.Reset() ' Resets Stopwatch
BtnStart.Enabled = False ' Disables Start button
Timer1.Enabled = False ' Turn the Timer off
EventFile.WriteLine(Label1.Text & vbTab & "Stop Acquisition") ' Writes event type to text file
TextBox1.AppendText(Label1.Text & vbTab & "Stop Acquisition" & vbNewLine) ' Inputs event type and hit return
Label1.Text = "0" ' Resets Stopwatch counter on screen
BtnStart.Text = "Start" ' Button text reverts to Start
EventFile.Close() ' Closes Text File
TextBox1.Focus() ' Restores focus to textbox after button press
End If
End Sub
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles TextBox1.KeyDown
If KeyPressed = False Then
KeyPressed = True
If e.KeyData = Keys.D1 Then ' If keystroke is "1"
e.SuppressKeyPress = True ' Prevents actual keypress appearing
EventFile.WriteLine(Label1.Text & vbTab & "Sigh") ' Writes timestamp and event type to text file
TextBox1.AppendText(Label1.Text & vbTab & "Sigh" & vbNewLine) ' Displays timestamp and event type on program textbox and hit return
ElseIf e.KeyData = Keys.D2 Then
e.SuppressKeyPress = True
EventFile.WriteLine(Label1.Text & vbTab & "Cry")
TextBox1.AppendText(Label1.Text & vbTab & "Cry" & vbNewLine)
ElseIf e.KeyData = Keys.D3 Then
e.SuppressKeyPress = True
EventFile.WriteLine(Label1.Text & vbTab & "Whimper")
TextBox1.AppendText(Label1.Text & vbTab & "Whimper" & vbNewLine)
ElseIf e.KeyData = Keys.D4 Then
e.SuppressKeyPress = True
EventFile.WriteLine(Label1.Text & vbTab & "Pant")
TextBox1.AppendText(Label1.Text & vbTab & "Pant" & vbNewLine)
ElseIf e.KeyData = Keys.D5 Then
e.SuppressKeyPress = True
EventFile.WriteLine(Label1.Text & vbTab & "Gag")
TextBox1.AppendText(Label1.Text & vbTab & "Gag" & vbNewLine)
ElseIf e.KeyData = Keys.D6 Then
e.SuppressKeyPress = True
EventFile.WriteLine(Label1.Text & vbTab & "Congested Breathing")
TextBox1.AppendText(Label1.Text & vbTab & "Congested Breathing" & vbNewLine)
ElseIf e.KeyData = Keys.D7 Then
e.SuppressKeyPress = True
EventFile.WriteLine(Label1.Text & vbTab & "Cough")
TextBox1.AppendText(Label1.Text & vbTab & "Cough" & vbNewLine)
ElseIf e.KeyData = Keys.D8 Then
e.SuppressKeyPress = True
EventFile.WriteLine(Label1.Text & vbTab & "Hiccup")
TextBox1.AppendText(Label1.Text & vbTab & "Hiccup" & vbNewLine)
ElseIf e.KeyData = Keys.D9 Then
e.SuppressKeyPress = True
EventFile.WriteLine(Label1.Text & vbTab & "Sneeze")
TextBox1.AppendText(Label1.Text & vbTab & "Sneeze" & vbNewLine)
ElseIf e.KeyData = Keys.D0 Then
e.SuppressKeyPress = True
EventFile.WriteLine(Label1.Text & vbTab & "Grunt")
TextBox1.AppendText(Label1.Text & vbTab & "Grunt" & vbNewLine)
Else
e.SuppressKeyPress = True ' Supresses keystroke entry when invalid key is pressed
End If
Else
e.SuppressKeyPress = True ' Supresses repeated keystroke entries when a valid key is held down
End If
End Sub
Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
KeyPressed = False ' Resets KeyPressed variable to False when key is released
End Sub
End Class
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
|