嵌入式linux上QT標準鍵盤輸入的實現

來源:互聯網
上載者:User

作者:劉洪濤,華清遠見嵌入式學院講師。

在嵌入式平台上運行QTE時,使用的鍵盤通常不是標準鍵盤,而是嵌入式裝置外擴的普通按鍵。那麼實現QTE鍵盤輸入的方法大體上可以分為兩類:

 

(1)編寫一個普通按鍵驅動,然後開闢一個QT線程讀取按索引值,在通過訊號把按索引值發送出去。需要接收鍵盤輸入的目標,聲明槽函數,接收鍵盤訊號。
(2)將按鍵驅動編寫成標準鍵盤驅動,讓QTE感覺和標準鍵盤在打交道。

上述兩種方法給有特點。我在一些項目中多數都是使用第1種方式,感覺比較直觀容易控制。但也有些情況要選用第2種方法。

第1種方法的實現比較容易,這裡就不多說了。下面主要把第2種方法的實現流程說明一下。

具體實現標準鍵盤輸入的過程可以分為兩步:

(1) 找一個標準usb鍵盤,測試QTE能否正確設別標準鍵盤
        (2) 編寫按鍵驅動,類比標準鍵盤輸入

一、第1步的實現過程:

●    配置QTE支援標準USB鍵盤
        配置qte庫時,增加鍵盤支援的參數,如下:
        ./configure …… -qt-kbd-usb ……
        ●    配置核心支援USB鍵盤輸入
        ●    插入USB鍵盤後,產生event裝置節點,如/dev/event2
        ●    設定QTE關聯的鍵盤裝置的環境變數
                export QWS_KEYBOARD=USB:/dev/event2
        ●    編寫一個接收鍵盤事件的QT測試代碼。

class MyDialog : public QDialog
        {
                ……
        protected:
                virtual void keyPressEvent(QKeyEvent *k);
        };

void MyDialog::keyPressEvent(QKeyEvent *k)
        {
                qDebug("in press event %x",k->key());
        }

●    測試鍵盤輸入

當按下F1~F12時,列印出:
        in press event 1000030
        in press event 1000031
        in press event 1000032
        in press event 1000033
        in press event 1000034
        in press event 1000035
        in press event 1000036
        in press event 1000037
        in press event 1000038
        in press event 1000039
        in press event 100003a

查QT協助 Key_F1=0x1000030

說明QDialog 的keyPressEvent可以接收到它能擷取的鍵盤訊號,即QTE和USB鍵盤串連正確。

二、第2步的實現過程:

主要參考/driver/usb/input/usbkbd.c程式,完成鍵盤類比。程式主要思想是編寫一個支援EV_KEY的input裝置驅動。下面摘取關鍵代碼。

●    完成input裝置的註冊、登出

struct input_dev *input_dev;
        static unsigned char usb_kbd_keycode[256] = {
                0, 0, 0, 0, 30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38,
              50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44, 2, 3,
                4, 5, 6, 7, 8, 9, 10, 11, 28, 1, 14, 15, 57, 12, 13, 26,
               27, 43, 43, 39, 40, 41, 51, 52, 53, 58, 59, 60, 61, 62, 63, 64,
              65, 66, 67, 68, 87, 88, 99, 70,119,110,102,104,111,107,109,106,
            105,108,103, 69, 98, 55, 74, 78, 96, 79, 80, 81, 75, 76, 77, 71,
              72, 73, 82, 83, 86,127,116,117,183,184,185,186,187,188,189,190,
            191,192,193,194,134,138,130,132,128,129,131,137,133,135,136,113,
            115,114, 0, 0, 0,121, 0, 89, 93,124, 92, 94, 95, 0, 0, 0,
            122,123, 90, 91, 85, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
              29, 42, 56,125, 97, 54,100,126,164,166,165,163,161,115,114,113,
            150,158,159,128,136,177,178,176,142,152,173,140
        };

/*初始化*/

static int button_init(void)
         {
         ……
                input_dev = input_allocate_device();//分配input裝置
                input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_LED) | BIT(EV_REP);
                // EV_KEY為要支援的鍵盤事件
                for (i = 0; i < 255; i++)
                         set_bit(usb_kbd_keycode[i], input_dev->keybit);
                 //設定支援的鍵盤碼,可根據實際情況註冊
                 input_register_device(input_dev);//註冊input裝置

}

/*登出*/

static void __exit button_cleanup(void)
        {
        ……
                input_unregister_device(input_dev);//登出input裝置
        }

●    中斷處理過程中完成鍵盤值的擷取及input事件的遞交

static irqreturn_t button_irq(int irq, void *dev_id, struct pt_regs *regs)
        {
        ……
                input_report_key(input_dev, 59, 1); //類比鍵盤碼F1按下過程
                input_report_key(input_dev, 59, 0);
                input_sync(input_dev);
        }
        /*在核心include/linux/input.h中
        #define KEY_F1 59
        */

上面給出了簡要的過程,大家在具體實現過程中多參考/driver/usb/input/usbkbd.c檔案,及注意按鍵去抖等問題。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.