Results 1 to 28 of 28

Thread: [RESOLVED] Creating Patient Event Logger for a Hospital NICU

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2011
    Posts
    31

    Resolved [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!

  2. #2
    Member
    Join Date
    May 2010
    Posts
    60

    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.

  3. #3
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    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?

  4. #4
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    865

    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.

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

  5. #5
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    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.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Dec 2011
    Posts
    31

    Re: Creating Patient Event Logger for a Hospital NICU

    Quote Originally Posted by yo mismo View Post
    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.
    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?


    Quote Originally Posted by Grimfort View Post
    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.


    Quote Originally Posted by BlindSniper View Post
    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.


    Quote Originally Posted by Merrion View Post
    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!

  7. #7
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Creating Patient Event Logger for a Hospital NICU

    Quote Originally Posted by Mudcake View Post
    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 .

  8. #8
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    865

    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.

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

  9. #9
    Hyperactive Member
    Join Date
    Jul 2011
    Posts
    278

    Re: Creating Patient Event Logger for a Hospital NICU

    Quote Originally Posted by yo mismo View Post
    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.
    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:
    1. Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles TextBox1.KeyDown
    2.        
    3. End Sub

    And then within that sub include the below (per key you wish to control)

    vbnet Code:
    1. If e.KeyData = Keys.W Then
    2.             MsgBox("Cough")
    3. 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.

  10. #10
    Hyperactive Member
    Join Date
    Jul 2011
    Posts
    278

    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.

  11. #11
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    865

    Re: Creating Patient Event Logger for a Hospital NICU

    Quote Originally Posted by OhGreen View Post
    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?

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

  12. #12
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    865

    Re: Creating Patient Event Logger for a Hospital NICU

    Double Post
    Last edited by BlindSniper; Dec 16th, 2011 at 11:18 AM.

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

  13. #13
    Hyperactive Member
    Join Date
    Jul 2011
    Posts
    278

    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.

  14. #14
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    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

  15. #15
    Hyperactive Member
    Join Date
    Jul 2011
    Posts
    278

    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:
    1. If e.KeyData = Keys.W Then
    2.             MsgBox(TimeOfDay & " " & "Cough")
    3. End If

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Dec 2011
    Posts
    31

    Re: Creating Patient Event Logger for a Hospital NICU

    Quote Originally Posted by Grimfort View Post
    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.

    Quote Originally Posted by OhGreen View Post
    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:
    1. Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles TextBox1.KeyDown
    2.  
    3.         If e.KeyData = Keys.Q Then                                  ' If Keypress is Q
    4.             e.SuppressKeyPress = True                               ' Prevents actual keypress appearing
    5.             TextBox1.AppendText("Movement" & vbNewLine)             ' Inputs event type and hit return
    6.         ElseIf e.KeyData = Keys.W Then
    7.             e.SuppressKeyPress = True
    8.             TextBox1.AppendText("Cough" & vbNewLine)
    9.         ElseIf e.KeyData = Keys.E Then
    10.             e.SuppressKeyPress = True
    11.             TextBox1.AppendText("Cry" & vbNewLine)
    12.         End If
    13.     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:
    1. Private Sub BtnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStart.Click
    2.         If Timer1.Enabled = False Then                              ' If the Timer isn't running
    3.             Timer1.Enabled = True                                   ' Turn the Timer on
    4.             BtnStart.Text = "Stop"                                  ' Button text now says Stop
    5.             TextBox1.Focus()                                        ' Restore focus to textbox after button press
    6.         Else                                                        ' If Timer is running
    7.             Timer1.Enabled = False                                  ' Turn the Timer off
    8.             BtnStart.Text = "Start"                                 ' Button text reverts to Start
    9.             TextBox1.Focus()                                        ' Restores focus to textbox after button press
    10.         End If
    11.     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.

  17. #17

    Thread Starter
    Junior Member
    Join Date
    Dec 2011
    Posts
    31

    Re: Creating Patient Event Logger for a Hospital NICU

    Quote Originally Posted by OhGreen View Post
    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:
    1. Public Class EventLogger
    2.     ' This program is designed to read keyboard inputs and output the corresponding patient events to a plain text file
    3.     ' Development commenced: December 15 2011
    4.  
    5.     Dim StopWatch As New Diagnostics.Stopwatch
    6.     Dim EventFile As System.IO.StreamWriter
    7.  
    8.     Private Sub EventLogger_Load1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    9.         Timer1.Enabled = False                                                              ' Runs on load and disables Timer till Start is clicked
    10.         Label1.Text = "0"                                                                   ' Sets Stopwatch counter on screen to 0 at the Start
    11.         EventFile = My.Computer.FileSystem.OpenTextFileWriter("C:\Eventuality\PatientX.states", True)
    12.     End Sub
    13.  
    14.     Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles TextBox1.KeyDown
    15.         If e.KeyData >= Keys.A And e.KeyData <= Keys.Z Then
    16.             If e.KeyData = Keys.Q Then                                                          ' If Keypress is Q
    17.                 EventFile.WriteLine(Label1.Text & vbTab & "Movement")                           ' Writes event type to text file
    18.                 e.SuppressKeyPress = True                                                       ' Prevents actual keypress appearing
    19.                 TextBox1.AppendText(Label1.Text & vbTab & "Movement" & vbNewLine)               ' Inputs event type and hit return
    20.             ElseIf e.KeyData = Keys.W Then
    21.                 EventFile.WriteLine(Label1.Text & vbTab & "Cough")
    22.                 e.SuppressKeyPress = True
    23.                 TextBox1.AppendText(Label1.Text & vbTab & "Cough" & vbNewLine)
    24.             ElseIf e.KeyData = Keys.E Then
    25.                 EventFile.WriteLine(Label1.Text & vbTab & "Cry")
    26.                 e.SuppressKeyPress = True
    27.                 TextBox1.AppendText(Label1.Text & vbTab & "Cry" & vbNewLine)
    28.             Else
    29.                 e.SuppressKeyPress = True
    30.             End If
    31.         Else
    32.             e.SuppressKeyPress = True
    33.         End If
    34.     End Sub
    35.  
    36.     Private Sub BtnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStart.Click
    37.  
    38.         If Timer1.Enabled = False Then                                                      ' If the Timer isn't running
    39.             Timer1.Enabled = True                                                           ' Turn the Timer on
    40.             Me.StopWatch.Start()                                                            ' Inits Stopwatch
    41.             EventFile.WriteLine(Label1.Text & vbTab & "Start Acquisition")                  ' Writes event type to text file
    42.             TextBox1.AppendText(Label1.Text & vbTab & "Start Acquisition" & vbNewLine)      ' Inputs event type and hit return
    43.             BtnStart.Text = "Stop"                                                          ' Button text now says Stop
    44.             TextBox1.Focus()                                                                ' Restore focus to textbox after button press
    45.         Else                                                                                ' If Timer is running
    46.             Me.StopWatch.Reset()                                                            ' Resets Stopwatch
    47.             Timer1.Enabled = False                                                          ' Turn the Timer off
    48.             EventFile.WriteLine(Label1.Text & vbTab & "Stop Acquisition")                   ' Writes event type to text file
    49.             TextBox1.AppendText(Label1.Text & vbTab & "Stop Acquisition" & vbNewLine)       ' Inputs event type and hit return
    50.             Label1.Text = "0"                                                               ' Resets Stopwatch counter on screen
    51.             BtnStart.Text = "Start"                                                         ' Button text reverts to Start
    52.             EventFile.Close()                                                               ' Closes Text File
    53.             TextBox1.Focus()                                                                ' Restores focus to textbox after button press
    54.         End If
    55.     End Sub
    56.  
    57.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    58.         Dim elapsed As TimeSpan = Me.StopWatch.Elapsed                                              ' I don't quite know what this line does but hey, it works!
    59.         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
    60.     End Sub
    61. 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.

  18. #18
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    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

  19. #19

    Thread Starter
    Junior Member
    Join Date
    Dec 2011
    Posts
    31

    Re: Creating Patient Event Logger for a Hospital NICU

    Quote Originally Posted by wild_bill View Post
    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.

  20. #20
    Hyperactive Member
    Join Date
    Jul 2011
    Posts
    278

    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?

  21. #21

    Thread Starter
    Junior Member
    Join Date
    Dec 2011
    Posts
    31

    Re: Creating Patient Event Logger for a Hospital NICU

    Quote Originally Posted by OhGreen View Post
    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.

  22. #22
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Creating Patient Event Logger for a Hospital NICU

    Quote Originally Posted by Mudcake View Post
    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

  23. #23

    Thread Starter
    Junior Member
    Join Date
    Dec 2011
    Posts
    31

    Re: Creating Patient Event Logger for a Hospital NICU

    Quote Originally Posted by wild_bill View Post
    Code:
    Label1.Text = Me.StopWatch.Elapsed.TotalSeconds.ToString("0")
    Thank you, Wild Bill. That's perfect!

  24. #24
    Hyperactive Member
    Join Date
    Jul 2011
    Posts
    278

    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:
    1. Dim keypressed As Boolean
    2.  
    3.     Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles TextBox1.KeyDown
    4.         If keypressed = False Then
    5.             If e.KeyData = Keys.W Then
    6.                 MsgBox(TimeOfDay & " " & "Cough")
    7.                 keypressed = True
    8.             End If
    9.         End If
    10.  
    11.     End Sub
    12.  
    13.     Private Sub TextBox1_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
    14.         If e.KeyData = Keys.W Then
    15.             keypressed = False
    16.         End If
    17.     End Sub

  25. #25

    Thread Starter
    Junior Member
    Join Date
    Dec 2011
    Posts
    31

    Re: Creating Patient Event Logger for a Hospital NICU

    Quote Originally Posted by OhGreen View Post
    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!

  26. #26
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    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

  27. #27

    Thread Starter
    Junior Member
    Join Date
    Dec 2011
    Posts
    31

    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:
    1. Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles TextBox1.KeyDown
    2.         If KeyPressed = False Then
    3.             KeyPressed = True
    4.             If e.KeyData = Keys.D1 Then                                                         ' If Keypress is 1
    5.                 e.SuppressKeyPress = True                                                       ' Prevents actual keypress appearing
    6.                 EventFile.WriteLine(Label1.Text & vbTab & "Sigh")                               ' Writes event type to text file
    7.                 TextBox1.AppendText(Label1.Text & vbTab & "Sigh" & vbNewLine)                   ' Inputs event type and hit return
    8.             Else
    9.                 e.SuppressKeyPress = True                                                       ' Supresses all invalid keystrokes
    10.             End If
    11.         Else
    12.             e.SuppressKeyPress = True                                                           ' Supresses all invalid keystrokes
    13.         End If
    14.     End Sub
    15.  
    16. Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
    17.         KeyPressed = False
    18.     End Sub

  28. #28

    Thread Starter
    Junior Member
    Join Date
    Dec 2011
    Posts
    31

    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:
    1. Public Class EventLogger
    2.     ' Eventuality - Version 1.0
    3.     ' This program is designed to read keyboard inputs and output the corresponding patient events to a plain text file
    4.     ' Development commenced: December 15 2011
    5.     ' Program Release: December 19 2011
    6.  
    7.     Dim StopWatch As New Diagnostics.Stopwatch
    8.     Dim EventFile As System.IO.StreamWriter
    9.     Dim KeyPressed As Boolean = False
    10.  
    11.     Private Sub EventLogger_Load1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    12.         Timer1.Enabled = False                                                              ' Runs on load and disables Timer till Start is clicked
    13.         Label1.Text = "0"                                                                   ' Sets Stopwatch counter on screen to 0 at the Start
    14.         TextBox1.Text = vbNewLine & vbNewLine & vbNewLine & vbNewLine & vbNewLine & vbNewLine & vbNewLine & _
    15.             "                      Eventuality Ver: 1.0"
    16.         TextBox1.Enabled = False                                                            ' Temporarily disables input to Textbox
    17.  
    18.         If My.Computer.FileSystem.DirectoryExists("C:\Eventuality\") = False Then           ' Checks if Eventuality directory does not exist
    19.             My.Computer.FileSystem.CreateDirectory("C:\Eventuality\")                       ' If it does not, create it
    20.         End If
    21.         SaveFileDialog1.InitialDirectory = "C:\Eventuality\"                                ' Sets default directory
    22.         SaveFileDialog1.DefaultExt = "states"                                               ' Sets default file extension
    23.  
    24.         If SaveFileDialog1.ShowDialog() = DialogResult.OK Then                              ' Get user defined filename and create file
    25.             Dim NameString As String = System.IO.Path.GetFileNameWithoutExtension(SaveFileDialog1.FileName)
    26.             EventFile = My.Computer.FileSystem.OpenTextFileWriter("C:\Eventuality\" & NameString & ".states", True)
    27.         Else
    28.             End                                                                             ' If user hits Cancel or does not enter a filename, program terminates
    29.         End If
    30.     End Sub
    31.  
    32.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    33.         Label1.Text = Me.StopWatch.Elapsed.TotalSeconds.ToString("0")                       ' Formats the text string in Label to display the elapsed time in seconds
    34.     End Sub
    35.  
    36.     Private Sub BtnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStart.Click
    37.  
    38.         If Timer1.Enabled = False Then                                                      ' If the Timer isn't running
    39.             TextBox1.Enabled = True                                                         ' Enables Textbox for input
    40.             TextBox1.Text = ""                                                              ' Resets Textbox contents
    41.             Timer1.Enabled = True                                                           ' Turn the Timer on
    42.             Me.StopWatch.Start()                                                            ' Inits Stopwatch
    43.             EventFile.WriteLine(Label1.Text & vbTab & "Start Acquisition")                  ' Writes event type to text file
    44.             TextBox1.AppendText(Label1.Text & vbTab & "Start Acquisition" & vbNewLine)      ' Inputs event type and hit return
    45.             BtnStart.Text = "Stop"                                                          ' Button text now says Stop
    46.             TextBox1.Focus()                                                                ' Restore focus to textbox after button press
    47.         Else                                                                                ' If Timer is running
    48.             Me.StopWatch.Reset()                                                            ' Resets Stopwatch
    49.             BtnStart.Enabled = False                                                        ' Disables Start button
    50.             Timer1.Enabled = False                                                          ' Turn the Timer off
    51.             EventFile.WriteLine(Label1.Text & vbTab & "Stop Acquisition")                   ' Writes event type to text file
    52.             TextBox1.AppendText(Label1.Text & vbTab & "Stop Acquisition" & vbNewLine)       ' Inputs event type and hit return
    53.             Label1.Text = "0"                                                               ' Resets Stopwatch counter on screen
    54.             BtnStart.Text = "Start"                                                         ' Button text reverts to Start
    55.             EventFile.Close()                                                               ' Closes Text File
    56.             TextBox1.Focus()                                                                ' Restores focus to textbox after button press
    57.         End If
    58.     End Sub
    59.  
    60.     Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles TextBox1.KeyDown
    61.         If KeyPressed = False Then
    62.             KeyPressed = True
    63.             If e.KeyData = Keys.D1 Then                                                         ' If keystroke is "1"
    64.                 e.SuppressKeyPress = True                                                       ' Prevents actual keypress appearing
    65.                 EventFile.WriteLine(Label1.Text & vbTab & "Sigh")                               ' Writes timestamp and event type to text file
    66.                 TextBox1.AppendText(Label1.Text & vbTab & "Sigh" & vbNewLine)                   ' Displays timestamp and event type on program textbox and hit return
    67.             ElseIf e.KeyData = Keys.D2 Then
    68.                 e.SuppressKeyPress = True
    69.                 EventFile.WriteLine(Label1.Text & vbTab & "Cry")
    70.                 TextBox1.AppendText(Label1.Text & vbTab & "Cry" & vbNewLine)
    71.             ElseIf e.KeyData = Keys.D3 Then
    72.                 e.SuppressKeyPress = True
    73.                 EventFile.WriteLine(Label1.Text & vbTab & "Whimper")
    74.                 TextBox1.AppendText(Label1.Text & vbTab & "Whimper" & vbNewLine)
    75.             ElseIf e.KeyData = Keys.D4 Then
    76.                 e.SuppressKeyPress = True
    77.                 EventFile.WriteLine(Label1.Text & vbTab & "Pant")
    78.                 TextBox1.AppendText(Label1.Text & vbTab & "Pant" & vbNewLine)
    79.             ElseIf e.KeyData = Keys.D5 Then
    80.                 e.SuppressKeyPress = True
    81.                 EventFile.WriteLine(Label1.Text & vbTab & "Gag")
    82.                 TextBox1.AppendText(Label1.Text & vbTab & "Gag" & vbNewLine)
    83.             ElseIf e.KeyData = Keys.D6 Then
    84.                 e.SuppressKeyPress = True
    85.                 EventFile.WriteLine(Label1.Text & vbTab & "Congested Breathing")
    86.                 TextBox1.AppendText(Label1.Text & vbTab & "Congested Breathing" & vbNewLine)
    87.             ElseIf e.KeyData = Keys.D7 Then
    88.                 e.SuppressKeyPress = True
    89.                 EventFile.WriteLine(Label1.Text & vbTab & "Cough")
    90.                 TextBox1.AppendText(Label1.Text & vbTab & "Cough" & vbNewLine)
    91.             ElseIf e.KeyData = Keys.D8 Then
    92.                 e.SuppressKeyPress = True
    93.                 EventFile.WriteLine(Label1.Text & vbTab & "Hiccup")
    94.                 TextBox1.AppendText(Label1.Text & vbTab & "Hiccup" & vbNewLine)
    95.             ElseIf e.KeyData = Keys.D9 Then
    96.                 e.SuppressKeyPress = True
    97.                 EventFile.WriteLine(Label1.Text & vbTab & "Sneeze")
    98.                 TextBox1.AppendText(Label1.Text & vbTab & "Sneeze" & vbNewLine)
    99.             ElseIf e.KeyData = Keys.D0 Then
    100.                 e.SuppressKeyPress = True
    101.                 EventFile.WriteLine(Label1.Text & vbTab & "Grunt")
    102.                 TextBox1.AppendText(Label1.Text & vbTab & "Grunt" & vbNewLine)
    103.             Else
    104.                 e.SuppressKeyPress = True                                                       ' Supresses keystroke entry when invalid key is pressed
    105.             End If
    106.         Else
    107.             e.SuppressKeyPress = True                                                           ' Supresses repeated keystroke entries when a valid key is held down
    108.         End If
    109.     End Sub
    110.  
    111.     Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
    112.         KeyPressed = False                                                                      ' Resets KeyPressed variable to False when key is released
    113.     End Sub
    114. 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
  •  



Click Here to Expand Forum to Full Width