作者 主題: 新手請問鍵盤問題  (閱讀 15772 次)

0 會員 與 1 訪客 正在閱讀本文。

LeonH

  • 可愛的小學生
  • *
  • 文章數: 3
    • 檢視個人資料
新手請問鍵盤問題
« 於: 2011-02-02 23:28 »
大家好我是超級新手,目前自學中,教材是How to think like a computer scientist
下面是範例程式碼:
代碼: (python) [選擇]
from gasp import *

begin_graphics(800, 600, title="Catch", background=color.YELLOW)
set_speed(120)

mitt_x = 780
mitt_y = 300
mitt = Circle((mitt_x, mitt_y), 20)

while True:
    if key_pressed('k') and mitt_y <= 580:
        mitt_y += 5
    elif key_pressed('j') and mitt_y >= 20:
        mitt_y -= 5

    if key_pressed('escape'):
        break

    move_to(mitt, (mitt_x, mitt_y))
    update_when('next_tick')

end_graphics()

可是實際在跑時,按j或k都可以改變mitt的座標,唯讀按esc就完全沒效果 ???
平台是linux mint、python 2.6.6

lefthaha

  • 可愛的小學生
  • *
  • 文章數: 7
    • 檢視個人資料
回覆: 新手請問鍵盤問題
« 回覆 #1 於: 2011-02-07 21:50 »
代碼: (python) [選擇]
from gasp import *

begin_graphics(800, 600, title="Catch", background=color.YELLOW)
set_speed(120)

mitt_x = 780
mitt_y = 300
mitt = Circle((mitt_x, mitt_y), 20)

while True:
    if key_pressed('k') and mitt_y <= 580:
        mitt_y += 5
    elif key_pressed('j') and mitt_y >= 20:
        mitt_y -= 5

    if key_pressed('escape'):
        break
#     ^^^^
# 因為 break 會直接跳出這個 while 迴圈,不繼續執行下面的 move_to() 和 update_when()
# 所以按了 esc 後就只會執行最後一行的 end_graphics(),不會改變 mitt 的座標
    move_to(mitt, (mitt_x, mitt_y))
    update_when('next_tick')

end_graphics()

LeonH

  • 可愛的小學生
  • *
  • 文章數: 3
    • 檢視個人資料
回覆: 新手請問鍵盤問題
« 回覆 #2 於: 2011-02-07 22:02 »
不好意思你可能誤會了⋯
按j或k是有效的
無效的是escape,按下去正常應該會跳出while迴圈然後接著執行end_graphics()
可是實際上按下escape完全沒作用

這段程式是內文的範例,在http://openbookproject.net/thinkcs/python/english2e/ch08.html#responding-to-the-keyboard

lefthaha

  • 可愛的小學生
  • *
  • 文章數: 7
    • 檢視個人資料
回覆: 新手請問鍵盤問題
« 回覆 #3 於: 2011-02-12 17:52 »
問題可能是出在 gasp 上面,看了一下程式碼,在 backend.py 裡面的 269 行那邊
http://bazaar.launchpad.net/~gasp-dev/gasp-core/main/view/head:/gasp/backend.py#L269

代碼: (python) [選擇]
def key_pressed(widget, event):
    if screen.text_entry:
        if event.keyval == 65293:
            screen.entry_done.put(screen.text_entry.entry)
            screen.text_entry = None
        elif event.keyval == 65288:
            screen.text_entry.entry = screen.text_entry.entry[:-1]
        else:
            screen.text_entry.entry += event.string
    else:
        string = ""
        if event.keyval == 65364:string = "down"
        elif event.keyval == 65362:string = "up"
        elif event.keyval == 65361:string = "left"
        elif event.keyval == 65363:string = "right"
        elif event.keyval == 65293:string = "enter"
        elif event.keyval == 65506:string = "shift"
        elif event.keyval == 65289:string = "tab"
        else:string = event.string
        if not string in screen.keys_pressed: screen.keys.put([string,"pressed"])
        if screen.update_when == KEY_PRESSED: screen.update2.put(NOTHING)

在 key_pressed() 裡面是利用 event.keyval 的值來判斷輸入的是哪些特殊鍵,但是沒有設定 "escape"
自己加上去以後,測試起來就正常了

代碼: (python) [選擇]
       
        elif event.keyval == 65506:string = "shift"
        elif event.keyval == 65289:string = "tab"
        #***************** 自己加上下面這行
        # test for escape
        elif event.keyval == 65307:string = "escape"
        #*****************
        else:string = event.string
        # 按下 escape 所回傳的 event.string 並不是 "escape",所以抓不到,必須自己設定
        if not string in screen.keys_pressed: screen.keys.put([string,"pressed"])
        if screen.update_when == KEY_PRESSED: screen.update2.put(NOTHING)

各個按鍵的 keyval 和 string 可以用下面這段程式測試

代碼: (python) [選擇]
#! /usr/bin/env python2
# test gtk key press event

import gtk

win = gtk.Window()

def kp(widget, event):
    print "\nYou pressed a key"
    print "Key Value:", event.keyval
    print "Key Strint:", event.string

win.connect("key-press-event", kp)

win.show()
gtk.main()

之前沒有寫過 gtk 的程式,網路上找範例湊合的,如果有錯誤的地方,再麻煩不吝指教,謝謝
« 上次編輯: 2011-02-12 22:50 由 lefthaha »

LeonH

  • 可愛的小學生
  • *
  • 文章數: 3
    • 檢視個人資料
回覆: 新手請問鍵盤問題
« 回覆 #4 於: 2011-02-12 20:11 »
太感謝了!原來問題出在GASP啊⋯