开发者

Simple UI to capture data

开发者 https://www.devze.com 2023-02-19 02:14 出处:网络
I know that this is a vague question, but I was hoping to get some help.I know VBA pretty well, and have been able to accomplish some simple tasks in python as well as the statistical programming lang

I know that this is a vague question, but I was hoping to get some help. I know VBA pretty well, and have been able to accomplish some simple tasks in python as well as the statistical programming language in R.

What I am looking to do is create a simple application that lets me capture data, some of which is ca开发者_JAVA百科ptured from the keyboard. Every time there is a keystroke, I wanted to create a new record in my dataset.

For some context, think about creating a simple interface that lets me track the location (and duration) of the puck in an NHL hockey game.

I am not really a programmer, but know just enough to get in trouble and am not really sure where to get started. I simply am looking for some thoughts on a very basic (non-commercial) solution.

Many thanks in advance.

EDIT: I want to capture how long the puck is each zone. I plan on using the directional keys left/right to "follow" the puck from zone to each. Each time the puck changes to a zone, I want to "close" the active record and start a new one. The start and end times will let me calculate how long the puck was in the zone. I also need a way to stop the creation of a new record for things like faceoffs, tv time outs, and end of period. I was planning on using the spacebar. My thought is that if I do this correctly, when I follow along, the times recorded should match up with what is posted on the game clock found on tv. Yes, this is a crazy idea.


If you choose to program in Python:

You could use the pygame package to easily capture keyboard events. The library was built to write games, but would probably give you the functionality that you are looking for with keydown/keyup events. It also handles mouse events and (since it is intended for games) has the ability to do graphics/text. The documentation is really good and it is cross platform. A possible downside is that you have to have a "screen" and it has to have focus. Here is a small example:

import pygame

def main():
    """
    Pygame Example
    """
    pygame.init()
    screen = pygame.display.set_mode((200, 200)) 
    app_running = True
    while app_running:
        # Get all key/mouse events from system.
        events = pygame.event.get()
        # Loop thru each event...
        for e in events:
            # Handle when the program is killed.
            if e.type == pygame.QUIT:
                app_running = False
                break
            # Handle key events.
            elif e.type == pygame.KEYDOWN:
                # Exit if escape is pressed.
                if e.key == pygame.K_ESCAPE:
                    app_running = False
                # Do something when the right arrow
                # is pressed.
                elif e.key == pygame.K_RIGHT:
                    print "right arrow pressed"
                # Do something when the left arrow
                # is pressed.
                elif e.key == pygame.K_LEFT:
                    print "left arrow pressed"
                # and so on ...
        # Fill the screen to blank it.
        #screen.fill(mycolor)
        # Write someting to the screen to display.
        #screen.blit(some_image, some_position)
        # Flip to display.
        #screen.flip()
    pygame.quit()

if __name__ == '__main__': 
    main() 

If you are using a version of Windows you could use the msvcrt library but the event handling is not as nice as pygame: instead of events, you have to deal with raw keyboard output and it is a little less intuitive. Here is a small code snippet from Robert Gillies on ActiveState:

import msvcrt

def funkeypress():
    """
    Waits for the user to press any key including function keys. Returns 
    the ascii code for the key or the scancode for the function key.
    """
    while 1:
        if msvcrt.kbhit():                  # Key pressed?
            a = ord(msvcrt.getch())         # get first byte of keyscan code  
            if a == 0 or a == 224:          # is it a function key?
                b = ord(msvcrt.getch())     # get next byte of key scan code
                x = a + (b*256)             # cook it.
                return x                    # return cooked scancode
            else:
                return a                    # else return ascii code


Look at scan() for keyboard input in R. And you didn't ask about mouse input, but consider locator() for that.

Put it a loop if you want the output immediately.


Is it necessary that you program it yourself? There is a free program called jwatcher Designed for scoring animal behavior in ethological studies. It seems like that would be well-suited to your task.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号