2011-03-23 76 views
4

我知道這是一個模糊的問題,但我希望得到一些幫助。我知道VBA非常好,並且能夠在Python中完成一些簡單的任務以及R中的統計編程語言。簡單的用戶界面來捕獲數據

我期望做的是創建一個簡單的應用程序,它可以讓我捕獲數據,這是從鍵盤捕獲的。每次有按鍵時,我都想在數據集中創建一條新記錄。

對於某些情況下,考慮創建一個簡單的界面,讓我可以跟蹤NHL曲棍球比賽中冰球的位置(和持續時間)。

我不是一個真正的程序員,但知道剛夠惹麻煩,我真的不知道從哪裏開始。我只是在尋找一些關於非常基本(非商業)解決方案的想法。

非常感謝提前。

編輯:我想捕捉冰球多久是每個區域。我打算使用左/右方向鍵來「跟隨」冰球從區域到每個區域。每當冰球變成一個區域時,我想「關閉」活動記錄並開始一個新的記錄。開始和結束時間會讓我計算冰球在區域內的時間。我還需要一種方法來阻止創建新的記錄,如對峙,電視超時和期限結束。我打算使用空格鍵。我的想法是,如果我正確地做到了這一點,那麼當我跟隨時,記錄的時間應該與電視上發現的遊戲時鐘上的內容相匹配。是的,這是一個瘋狂的想法。

+1

你能給的類型,您想要獲取的數據的詳細信息,以及你希望怎樣發生的呢?你想捕捉任何類型的鍵盤輸入,或特定的字母? – 2011-03-23 19:20:22

+0

是的,理想情況下我可以使用箭頭鍵,但如果需要,我可能會適應它。 – Btibert3 2011-03-23 23:52:38

回答

1

如果你選擇Python編程:

您可以使用pygame包輕鬆捕捉鍵盤事件。該庫是爲編寫遊戲而設計的,但可能會爲您提供使用keydown/keyup事件尋找的功能。它還處理鼠標事件和(因爲它是用於遊戲)有能力做圖形/文字。文檔非常好,它是跨平臺的。一個可能的缺點是你必須有一個「屏幕」,它必須有重點。這裏是一個小例子:

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() 

如果您使用的Windows版本,你可以使用msvcrt庫,但該事件處理是不是像你一樣pygame:不是事件,你必須處理原始鍵盤輸出它有點不直觀。下面是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 
+0

這正是我所期待的!謝謝 – Btibert3 2011-03-24 23:06:58

0

查看R中鍵盤輸入的掃描()。並且您沒有詢問有關鼠標輸入的信息,但考慮使用locator()。

如果您想立即輸出,請將其設爲循環。

+0

沒有想到掃描。你能給我舉一個例子,說明如何用掃描捕捉左或右箭頭? Thx – Btibert3 2011-03-23 23:26:51

0

你有必要自己編程嗎?有一個名爲jwatcher的免費程序設計用於在行爲學研究中對動物行爲進行評分。看起來這很適合你的任務。