2023-05-29 用 fltk gui库编写一个打字练习程序

用 fltk gui库编写一个打字练习程序

  • 前言
  • 一、FLTK GUI 库
  • 二、使用步骤
    • 1.引入库
    • 2.使用代码
  • 总结


前言

给孩子练习键盘打字, 发现终端还是欠点意思, 研究了一下gui, 最终用 fltk库弄了一个.

对于没有接触过gui的人, 发现, 编程的逻辑和终端区别很大, 很繁琐, 可能需要适应适应, 另外, 文档很重要, 示例很重要.


一、FLTK GUI 库

FLTK is a cross-platform C++ GUI toolkit for UNIX®/Linux® (X11), Microsoft® Windows®, and macOS®. FLTK provides modern GUI functionality without the bloat and supports 3D graphics via OpenGL® and its built-in GLUT emulation.

FLTK is designed to be small and modular enough to be statically linked, but works fine as a shared library. FLTK also includes an excellent UI builder called FLUID that can be used to create applications in minutes.

FLTK is provided under the terms of the GNU Library Public License, Version 2 with exceptions that allow for static linking.

总之, 就是足够小, 跨平台, 可商用, 当然更具体的请去官网查看.

二、使用步骤

1.引入库

我使用msys2安装的, 因为比较容易, 我暂时不想自己编译.

以下是比较常用的头文件, 主要用于建立窗口, 输入输出框, 时钟, GUI也少不了图片解析, 比如jpg和png.

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

编译参数需要加上fltk的库文件

 E:\msys64\clang64\bin\clang++.exe -glldb  -lfltk -lfltk_images  test227.cpp p -o E:\clangC++\answer\C++\test227.exe 

2.使用代码

代码如下(示例):

头文件, 用于给出最基本的结构及函数, 为了能够catch键盘事件, 派生一个windowInput类, 改写handle函数.

// generated by Fast Light User Interface Designer (fluid) version 1.0308
#ifndef TYPING_H
#define TYPING_H

#include 
#include 
#include 
#include 
#include 
#include 

struct windowInput : Fl_Input
{
    windowInput(int X, int Y, int W, int H, const char *l = nullptr)
        : Fl_Input(X, Y, W, H, l)
    {}
    auto handle(int event) -> int override;
};

struct windowObject
{
    Fl_Double_Window *window;
    Fl_Box *box;
    windowInput *input;
    Fl_Output *displayText;
    Fl_Output *displayChar;
    Fl_Clock *clock;
};

auto makeWindow(const char *fileName) -> windowObject &;

auto openFile(const char *fileName) -> bool;

#endif

cpp文件, 装载函数实现, 但我要坦白, 由于对库不是特别了解, 使用了大量的全局数据, 但通过匿名的命名空间, 约束在cpp文件中, 尽量不污染全局.

gui本身不复杂, 但需要熟练, 文档和示例协助理解.

当然, 编程思路和终端编程还是有比较大的区别, 比如, 原先我想主程序由while循环和标准IO流驱动, 但发现, 在这种框架下是不可能的, 因为需要在进入gui框架循环前不能有任何阻塞, 否则就进入不了图形界面, 所以需要将循环部分揉进GUI框架的handle函数中, 而这个函数, 还不能传进数据, 只能用全局数据.

不过也可能是我用的实在不熟, 一天时间有点紧, 可能callback这种传数据的方法, 更符合这种编程模式, 只是我不太了解callback的运行逻辑(这个GUI框架的逻辑), 可能要看看源码.

// generated by Fast Light User Interface Designer (fluid) version 1.0308
#include "typingGUI.h"
#include 
#include 
#include 
#include 

namespace
{
windowObject winObj;

std::ifstream file;
std::string text;
std::array<char, 128> chrArr{"请输入: "};

int chr = 0xff8d;
int charIndex;

auto image_keyboard2(const char *fileName) -> Fl_Image *
{
    static Fl_Image *image = new Fl_PNG_Image(fileName);
    return image;
}

} // namespace

auto openFile(const char *fileName) -> bool
{
    file.open(fileName);
    return file.is_open();
}

auto windowInput::handle(int event) -> int
{
    if (event == FL_KEYDOWN || event == FL_KEYUP)
    {
        static int keycode;
        // keycode = Fl::event_key(); 这个最终给到的不是字母, 而是键盘编码, 大部分是一致的但涉及小键盘和shift就不同了
        keycode = *Fl::event_text();

        if (keycode == chr)
        {
            if (charIndex != text.size())
            {
                chr = text[charIndex];
                chrArr[11] = text[charIndex++];
                winObj.displayChar->value(chrArr.data());
                winObj.input->value(reinterpret_cast<char *>(&chr));
            }
            else if (file >> text)
            {
                charIndex = 0;
                chr = text[charIndex];
                chrArr[11] = text[charIndex++];
                winObj.displayText->value(text.c_str());
                winObj.displayChar->value(chrArr.data());
                winObj.input->value(reinterpret_cast<char *>(&chr));
            }
            else
            {
                winObj.displayText->value("恭喜过关, 请关闭窗口");
                file.close();
            }
        }
        else
        {
            winObj.input->value(reinterpret_cast<char *>(&keycode));
        }
    }
    return Fl_Input::handle(event);
}

auto makeWindow(const char *fileName) -> windowObject &
{
    winObj.window = new Fl_Double_Window(1920, 1017, "打字练习");
    winObj.window->resizable(winObj.window);

    // 获取系统中的字体数目
    Fl::set_fonts("-*");

    if (winObj.window != nullptr)
    {
        winObj.window->box(FL_UP_BOX);
        winObj.window->color(static_cast<Fl_Color>(25));

        {
            winObj.box = new Fl_Box(80, 23, 1720, 512);
            winObj.box->image(
                image_keyboard2(fileName)); //"E:\\clangC++\\keyboard2.png"
            winObj.box->labelfont(617);
            winObj.box->labelsize(53);
            winObj.box->labelcolor(static_cast<Fl_Color>(139));
        } // Fl_Box* box

        {
            winObj.input = new windowInput(276, 747, 1517, 84, "input:");
            winObj.input->labelfont(617);
            winObj.input->labelsize(72);
            winObj.input->textfont(617);
            winObj.input->textsize(72);
            winObj.input->align(static_cast<Fl_Align>(36));
        } // Fl_Input* input

        {
            winObj.displayText = new Fl_Output(83, 545, 1710, 84);
            winObj.displayText->box(FL_UP_BOX);
            winObj.displayText->labeltype(FL_NO_LABEL);
            winObj.displayText->labelfont(617);
            winObj.displayText->labelsize(72);
            winObj.displayText->textfont(617);
            winObj.displayText->textsize(72);
            winObj.displayText->value("按回车开始练习!");
        } // Fl_Text_Display* displayText

        {
            winObj.displayChar = new Fl_Output(83, 644, 1710, 84);
            winObj.displayChar->box(FL_UP_BOX);
            winObj.displayChar->labeltype(FL_NO_LABEL);
            winObj.displayChar->labelfont(617);
            winObj.displayChar->labelsize(72);
            winObj.displayChar->textfont(617);
            winObj.displayChar->textsize(72);
        } // Fl_Text_Display* displayChar

        {
            winObj.clock = new Fl_Clock(918, 849, 150, 150);
            winObj.clock->labelfont(617);
            winObj.clock->labelsize(24);
            winObj.clock->align((FL_ALIGN_CENTER));
        } // Fl_Clock* clock
        winObj.window->end();

        winObj.window->show();
    } // Fl_Double_Window* window

    return winObj;
}

2023-06-02 更新:

  1. 设置键盘提示
  2. auto windowInput::handle(int event) -> int 函数处理事件但未消除事件, 导致事件多次处理, 使得反应过慢, 无法快速捕获键盘.

头文件更新

// generated by Fast Light User Interface Designer (fluid) version 1.0308
#ifndef TYPING_H
#define TYPING_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 

struct windowInput : Fl_Input
{
    windowInput(int X, int Y, int W, int H, const char *l = nullptr)
        : Fl_Input(X, Y, W, H, l)
    {}
    auto handle(int event) -> int override;
};

struct windowObject
{
    Fl_Double_Window *window;
    Fl_Box *box;
    windowInput *input;
    Fl_Output *displayText;
    Fl_Output *displayChar;
    Fl_Clock *clock;
    std::array<Fl_Box *, 40> keys;
};

auto makeWindow(const char *fileName) -> windowObject &;

auto openFile(const char *fileName) -> bool;

#endif

cpp文件更新

// generated by Fast Light User Interface Designer (fluid) version 1.0308
#include "typingGUI.h"
#include 
#include 
#include 
#include 

#define KEYCOLOR 30
#define LABELSIZE 100
#define LABELCOLOR 7

namespace
{
windowObject winObj;

std::ifstream file;
std::string text;
std::array<char, 128> chrArr{"请输入: "};

std::array<const char *, 40> nameArr{"1!", "2@", "3#", "4$", "5%", "6^", "7&",
                                     "8*", "9(", "0)", "q",  "w",  "e",  "r",
                                     "t",  "y",  "u",  "i",  "o",  "p",  "a",
                                     "s",  "d",  "f",  "g",  "h",  "j",  "k",
                                     "l",  ";:", "z",  "x",  "c",  "v",  "b",
                                     "n",  "m",  ",<", ".>", "/?"};

int chr = 13;
int charIndex;

auto image_keyboard2(const char *fileName) -> Fl_Image *
{
    static Fl_Image *image = new Fl_PNG_Image(fileName);
    return image;
}

} // namespace

auto openFile(const char *fileName) -> bool
{
    file.open(fileName);
    return file.is_open();
}

void showColor(int chr)
{
    switch (chr)
    {
    case '1':
        winObj.keys[0]->show();
        break;
    case '2':
        winObj.keys[1]->show();
        break;
    case '3':
        winObj.keys[2]->show();
        break;
    case '4':
        winObj.keys[3]->show();
        break;
    case '5':
        winObj.keys[4]->show();
        break;
    case '6':
        winObj.keys[5]->show();
        break;
    case '7':
        winObj.keys[6]->show();
        break;
    case '8':
        winObj.keys[7]->show();
        break;
    case '9':
        winObj.keys[8]->show();
        break;
    case '0':
        winObj.keys[9]->show();
        break;
    case 'q':
    case 'Q':
        winObj.keys[10]->show();
        break;
    case 'w':
    case 'W':
        winObj.keys[11]->show();
        break;
    case 'e':
    case 'E':
        winObj.keys[12]->show();
        break;
    case 'r':
    case 'R':
        winObj.keys[13]->show();
        break;
    case 't':
    case 'T':
        winObj.keys[14]->show();
        break;
    case 'y':
    case 'Y':
        winObj.keys[15]->show();
        break;
    case 'u':
    case 'U':
        winObj.keys[16]->show();
        break;
    case 'i':
    case 'I':
        winObj.keys[17]->show();
        break;
    case 'o':
    case 'O':
        winObj.keys[18]->show();
        break;
    case 'p':
    case 'P':
        winObj.keys[19]->show();
        break;
    case 'a':
    case 'A':
        winObj.keys[20]->show();
        break;
    case 's':
    case 'S':
        winObj.keys[21]->show();
        break;
    case 'd':
    case 'D':
        winObj.keys[22]->show();
        break;
    case 'f':
    case 'F':
        winObj.keys[23]->show();
        break;
    case 'g':
    case 'G':
        winObj.keys[24]->show();
        break;
    case 'h':
    case 'H':
        winObj.keys[25]->show();
        break;
    case 'j':
    case 'J':
        winObj.keys[26]->show();
        break;
    case 'k':
    case 'K':
        winObj.keys[27]->show();
        break;
    case 'l':
    case 'L':
        winObj.keys[28]->show();
        break;
    case ';':
    case ':':
        winObj.keys[29]->show();
        break;
    case 'z':
    case 'Z':
        winObj.keys[30]->show();
        break;
    case 'x':
    case 'X':
        winObj.keys[31]->show();
        break;
    case 'c':
    case 'C':
        winObj.keys[32]->show();
        break;
    case 'v':
    case 'V':
        winObj.keys[33]->show();
        break;
    case 'b':
    case 'B':
        winObj.keys[34]->show();
        break;
    case 'n':
    case 'N':
        winObj.keys[35]->show();
        break;
    case 'm':
    case 'M':
        winObj.keys[36]->show();
        break;
    case ',':
    case '<':
        winObj.keys[37]->show();
        break;
    case '.':
    case '>':
        winObj.keys[38]->show();
        break;
    case '/':
    case '?':
        winObj.keys[39]->show();
        break;
    default:;
    }
}

void unshowColor(int chr)
{
    switch (chr)
    {
    case '1':
        winObj.keys[0]->hide();
        break;
    case '2':
        winObj.keys[1]->hide();
        break;
    case '3':
        winObj.keys[2]->hide();
        break;
    case '4':
        winObj.keys[3]->hide();
        break;
    case '5':
        winObj.keys[4]->hide();
        break;
    case '6':
        winObj.keys[5]->hide();
        break;
    case '7':
        winObj.keys[6]->hide();
        break;
    case '8':
        winObj.keys[7]->hide();
        break;
    case '9':
        winObj.keys[8]->hide();
        break;
    case '0':
        winObj.keys[9]->hide();
        break;
    case 'q':
    case 'Q':
        winObj.keys[10]->hide();
        break;
    case 'w':
    case 'W':
        winObj.keys[11]->hide();
        break;
    case 'e':
    case 'E':
        winObj.keys[12]->hide();
        break;
    case 'r':
    case 'R':
        winObj.keys[13]->hide();
        break;
    case 't':
    case 'T':
        winObj.keys[14]->hide();
        break;
    case 'y':
    case 'Y':
        winObj.keys[15]->hide();
        break;
    case 'u':
    case 'U':
        winObj.keys[16]->hide();
        break;
    case 'i':
    case 'I':
        winObj.keys[17]->hide();
        break;
    case 'o':
    case 'O':
        winObj.keys[18]->hide();
        break;
    case 'p':
    case 'P':
        winObj.keys[19]->hide();
        break;
    case 'a':
    case 'A':
        winObj.keys[20]->hide();
        break;
    case 's':
    case 'S':
        winObj.keys[21]->hide();
        break;
    case 'd':
    case 'D':
        winObj.keys[22]->hide();
        break;
    case 'f':
    case 'F':
        winObj.keys[23]->hide();
        break;
    case 'g':
    case 'G':
        winObj.keys[24]->hide();
        break;
    case 'h':
    case 'H':
        winObj.keys[25]->hide();
        break;
    case 'j':
    case 'J':
        winObj.keys[26]->hide();
        break;
    case 'k':
    case 'K':
        winObj.keys[27]->hide();
        break;
    case 'l':
    case 'L':
        winObj.keys[28]->hide();
        break;
    case ';':
    case ':':
        winObj.keys[29]->hide();
        break;
    case 'z':
    case 'Z':
        winObj.keys[30]->hide();
        break;
    case 'x':
    case 'X':
        winObj.keys[31]->hide();
        break;
    case 'c':
    case 'C':
        winObj.keys[32]->hide();
        break;
    case 'v':
    case 'V':
        winObj.keys[33]->hide();
        break;
    case 'b':
    case 'B':
        winObj.keys[34]->hide();
        break;
    case 'n':
    case 'N':
        winObj.keys[35]->hide();
        break;
    case 'm':
    case 'M':
        winObj.keys[36]->hide();
        break;
    case ',':
    case '<':
        winObj.keys[37]->hide();
        break;
    case '.':
    case '>':
        winObj.keys[38]->hide();
        break;
    case '/':
    case '?':
        winObj.keys[39]->hide();
        break;
    }
}

auto windowInput::handle(int event) -> int
{
    if (event == FL_KEYDOWN || event == FL_KEYUP)
    {
        static int keycode;
        // keycode = Fl::event_key();
        keycode = *Fl::event_text();

        if (keycode == chr)
        {
            unshowColor(chr);
            if (charIndex != text.size())
            {
                chr = text[charIndex];
                showColor(chr);
                chrArr[11] = text[charIndex++];
                winObj.displayChar->value(chrArr.data());
                winObj.input->value(reinterpret_cast<char *>(&chr));
                return 1;
            }
            if (file >> text)
            {
                charIndex = 0;
                chr = text[charIndex];
                showColor(chr);
                chrArr[11] = text[charIndex++];
                winObj.displayText->value(text.c_str());
                winObj.displayChar->value(chrArr.data());
                winObj.input->value(reinterpret_cast<char *>(&chr));
                return 1;
            }
            {
                unshowColor(chr);
                winObj.displayText->value("恭喜过关, 请关闭窗口");
                file.close();
                return 1;
            }
        }
        else
        {
            winObj.input->value(reinterpret_cast<char *>(&keycode));
            return 1;
        }
    }
    return Fl_Input::handle(event);
}

auto makeWindow(const char *fileName) -> windowObject &
{
    winObj.window = new Fl_Double_Window(1920, 1017, "打字练习");
    winObj.window->resizable(winObj.window);

    // 获取系统中的字体数目
    Fl::set_fonts("-*");

    if (winObj.window != nullptr)
    {
        winObj.window->box(FL_UP_BOX);
        winObj.window->color(static_cast<Fl_Color>(25));

        {
            winObj.box = new Fl_Box(80, 23, 1720, 512);
            winObj.box->image(
                image_keyboard2(fileName)); //"E:\\clangC++\\keyboard2.png"
            winObj.box->labelfont(617);
            winObj.box->labelsize(53);
            winObj.box->labelcolor(static_cast<Fl_Color>(139));
        } // Fl_Box* box

        {
            winObj.input = new windowInput(276, 747, 1517, 84, "input:");
            winObj.input->labelfont(617);
            winObj.input->labelsize(72);
            winObj.input->textfont(617);
            winObj.input->textsize(72);
            winObj.input->align(static_cast<Fl_Align>(36));
        } // Fl_Input* input

        {
            winObj.displayText = new Fl_Output(83, 545, 1710, 84);
            winObj.displayText->box(FL_UP_BOX);
            winObj.displayText->labeltype(FL_NO_LABEL);
            winObj.displayText->labelfont(617);
            winObj.displayText->labelsize(72);
            winObj.displayText->textfont(617);
            winObj.displayText->textsize(72);
            winObj.displayText->value("按回车开始练习!");
        } // Fl_Text_Display* displayText

        {
            winObj.displayChar = new Fl_Output(83, 644, 1710, 84);
            winObj.displayChar->box(FL_UP_BOX);
            winObj.displayChar->labeltype(FL_NO_LABEL);
            winObj.displayChar->labelfont(617);
            winObj.displayChar->labelsize(72);
            winObj.displayChar->textfont(617);
            winObj.displayChar->textsize(72);
        } // Fl_Text_Display* displayChar

        {
            winObj.clock = new Fl_Clock(918, 849, 150, 150);
            winObj.clock->labelfont(617);
            winObj.clock->labelsize(24);
            winObj.clock->align((FL_ALIGN_CENTER));
        } // Fl_Clock* clock
        {
            for (int i = 0; i != 10; i++)
            {
                winObj.keys[i] =
                    new Fl_Box(i * 114 + 205, 24, 110, 100, nameArr[i]);
            }
            for (int i = 0; i != 10; i++)
            {
                winObj.keys[i + 10] =
                    new Fl_Box(i * 114 + 260, 126, 110, 100, nameArr[i + 10]);
            }
            for (int i = 0; i != 10; i++)
            {
                winObj.keys[i + 20] =
                    new Fl_Box(i * 114 + 300, 228, 110, 100, nameArr[i + 20]);
            }
            for (int i = 0; i != 10; i++)
            {
                winObj.keys[i + 30] =
                    new Fl_Box(i * 114 + 375, 330, 110, 100, nameArr[i + 30]);
            }
            for (int i = 0; i != 40; i++)
            {
                winObj.keys[i]->labelsize(LABELSIZE);
                winObj.keys[i]->labelcolor(LABELCOLOR);
                winObj.keys[i]->box(FL_UP_BOX);
                winObj.keys[i]->color(KEYCOLOR);
                winObj.keys[i]->hide();
            }
        }
        winObj.window->end();

        winObj.window->show();
    } // Fl_Double_Window* window

    return winObj;
}

2023-06-02 更新 v3

  1. 不在使用图形键盘, 均由图形系统绘制

头文件

// generated by Fast Light User Interface Designer (fluid) version 1.0308
#ifndef TYPING_H
#define TYPING_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 

struct windowInput : Fl_Input
{
    windowInput(int X, int Y, int W, int H, const char *l = nullptr)
        : Fl_Input(X, Y, W, H, l)
    {}
    auto handle(int event) -> int override;
};

struct windowObject
{
    Fl_Double_Window *window;
    Fl_Box *box;
    windowInput *input;
    Fl_Output *displayText;
    Fl_Output *displayChar;
    Fl_Clock *clock;
    std::array<Fl_Box *, 61> keys;
};

auto makeWindow() -> windowObject &;

auto openFile(const char *fileName) -> bool;

#endif

cpp文件

// generated by Fast Light User Interface Designer (fluid) version 1.0308
#include "typingGUI.h"
#include 
#include 
#include 
#include 
#include 

#define KEYCOLOR 30
#define CHANGEKEYCOLOR 4
#define LABELSIZE 40
#define LABELCOLOR 7

namespace
{
windowObject winObj;

std::ifstream file;
std::string text;
std::array<char, 128> chrArr{"请输入: "};

std::array<const char *, 40> nameArr{"1!", "2@", "3#", "4$", "5%", "6^", "7&",
                                     "8*", "9(", "0)", "q",  "w",  "e",  "r",
                                     "t",  "y",  "u",  "i",  "o",  "p",  "a",
                                     "s",  "d",  "f",  "g",  "h",  "j",  "k",
                                     "l",  ";:", "z",  "x",  "c",  "v",  "b",
                                     "n",  "m",  ",<", ".>", "/?"};

struct keys
{
    const char *key;
    int x;
    int y;
    int w;
    int h;
};

std::array<keys, 21> keyArr{
    {{"~`", 90, 24, 110, 100},        {"_-", 1344, 24, 110, 100},
     {"+=", 1456, 24, 110, 100},      {"Backspace", 1570, 24, 226, 100},
     {"Tab", 90, 126, 165, 100},      {"{[", 1400, 126, 110, 100},
     {"}]", 1514, 126, 110, 100},     {"|\\", 1627, 126, 168, 100},
     {"CapsLock", 90, 228, 204, 100}, {"\" \'", 1438, 228, 110, 100},
     {"Enter", 1550, 228, 244, 100},  {"Shift", 90, 330, 280, 100},
     {"Shift", 1514, 330, 280, 100},  {"Ctrl", 90, 432, 208, 100},
     {"Win", 300, 432, 110, 100},     {"Alt", 414, 432, 110, 100},
     {"Space", 528, 432, 712, 100},   {"Alt", 1243, 432, 110, 100},
     {"Win", 1358, 432, 110, 100},    {"Type", 1472, 432, 110, 100},
     {"Ctrl", 1586, 432, 208, 100}}};

int chr = 13;

int charIndex = 0;

} // namespace

auto openFile(const char *fileName) -> bool
{
    file.open(fileName);
    return file.is_open();
}

void showColor(int chr)
{
    switch (chr)
    {
    case '1':
        winObj.keys[0]->color(CHANGEKEYCOLOR);
        winObj.keys[0]->redraw();
        break;
    case '2':
        winObj.keys[1]->color(CHANGEKEYCOLOR);
        winObj.keys[1]->redraw();
        break;
    case '3':
        winObj.keys[2]->color(CHANGEKEYCOLOR);
        winObj.keys[2]->redraw();
        break;
    case '4':
        winObj.keys[3]->color(CHANGEKEYCOLOR);
        winObj.keys[3]->redraw();
        break;
    case '5':
        winObj.keys[4]->color(CHANGEKEYCOLOR);
        winObj.keys[4]->redraw();
        break;
    case '6':
        winObj.keys[5]->color(CHANGEKEYCOLOR);
        winObj.keys[5]->redraw();
        break;
    case '7':
        winObj.keys[6]->color(CHANGEKEYCOLOR);
        winObj.keys[6]->redraw();
        break;
    case '8':
        winObj.keys[7]->color(CHANGEKEYCOLOR);
        winObj.keys[7]->redraw();
        break;
    case '9':
        winObj.keys[8]->color(CHANGEKEYCOLOR);
        winObj.keys[8]->redraw();
        break;
    case '0':
        winObj.keys[9]->color(CHANGEKEYCOLOR);
        winObj.keys[9]->redraw();
        break;
    case 'q':
    case 'Q':
        winObj.keys[10]->color(CHANGEKEYCOLOR);
        winObj.keys[10]->redraw();
        break;
    case 'w':
    case 'W':
        winObj.keys[11]->color(CHANGEKEYCOLOR);
        winObj.keys[11]->redraw();
        break;
    case 'e':
    case 'E':
        winObj.keys[12]->color(CHANGEKEYCOLOR);
        winObj.keys[12]->redraw();
        break;
    case 'r':
    case 'R':
        winObj.keys[13]->color(CHANGEKEYCOLOR);
        winObj.keys[13]->redraw();
        break;
    case 't':
    case 'T':
        winObj.keys[14]->color(CHANGEKEYCOLOR);
        winObj.keys[14]->redraw();
        break;
    case 'y':
    case 'Y':
        winObj.keys[15]->color(CHANGEKEYCOLOR);
        winObj.keys[15]->redraw();
        break;
    case 'u':
    case 'U':
        winObj.keys[16]->color(CHANGEKEYCOLOR);
        winObj.keys[16]->redraw();
        break;
    case 'i':
    case 'I':
        winObj.keys[17]->color(CHANGEKEYCOLOR);
        winObj.keys[17]->redraw();
        break;
    case 'o':
    case 'O':
        winObj.keys[18]->color(CHANGEKEYCOLOR);
        winObj.keys[18]->redraw();
        break;
    case 'p':
    case 'P':
        winObj.keys[19]->color(CHANGEKEYCOLOR);
        winObj.keys[19]->redraw();
        break;
    case 'a':
    case 'A':
        winObj.keys[20]->color(CHANGEKEYCOLOR);
        winObj.keys[20]->redraw();
        break;
    case 's':
    case 'S':
        winObj.keys[21]->color(CHANGEKEYCOLOR);
        winObj.keys[21]->redraw();
        break;
    case 'd':
    case 'D':
        winObj.keys[22]->color(CHANGEKEYCOLOR);
        winObj.keys[22]->redraw();
        break;
    case 'f':
    case 'F':
        winObj.keys[23]->color(CHANGEKEYCOLOR);
        winObj.keys[23]->redraw();
        break;
    case 'g':
    case 'G':
        winObj.keys[24]->color(CHANGEKEYCOLOR);
        winObj.keys[24]->redraw();
        break;
    case 'h':
    case 'H':
        winObj.keys[25]->color(CHANGEKEYCOLOR);
        winObj.keys[25]->redraw();
        break;
    case 'j':
    case 'J':
        winObj.keys[26]->color(CHANGEKEYCOLOR);
        winObj.keys[26]->redraw();
        break;
    case 'k':
    case 'K':
        winObj.keys[27]->color(CHANGEKEYCOLOR);
        winObj.keys[27]->redraw();
        break;
    case 'l':
    case 'L':
        winObj.keys[28]->color(CHANGEKEYCOLOR);
        winObj.keys[28]->redraw();
        break;
    case ';':
    case ':':
        winObj.keys[29]->color(CHANGEKEYCOLOR);
        winObj.keys[29]->redraw();
        break;
    case 'z':
    case 'Z':
        winObj.keys[30]->color(CHANGEKEYCOLOR);
        winObj.keys[30]->redraw();
        break;
    case 'x':
    case 'X':
        winObj.keys[31]->color(CHANGEKEYCOLOR);
        winObj.keys[31]->redraw();
        break;
    case 'c':
    case 'C':
        winObj.keys[32]->color(CHANGEKEYCOLOR);
        winObj.keys[32]->redraw();
        break;
    case 'v':
    case 'V':
        winObj.keys[33]->color(CHANGEKEYCOLOR);
        winObj.keys[33]->redraw();
        break;
    case 'b':
    case 'B':
        winObj.keys[34]->color(CHANGEKEYCOLOR);
        winObj.keys[34]->redraw();
        break;
    case 'n':
    case 'N':
        winObj.keys[35]->color(CHANGEKEYCOLOR);
        winObj.keys[35]->redraw();
        break;
    case 'm':
    case 'M':
        winObj.keys[36]->color(CHANGEKEYCOLOR);
        winObj.keys[36]->redraw();
        break;
    case ',':
    case '<':
        winObj.keys[37]->color(CHANGEKEYCOLOR);
        winObj.keys[37]->redraw();
        break;
    case '.':
    case '>':
        winObj.keys[38]->color(CHANGEKEYCOLOR);
        winObj.keys[38]->redraw();
        break;
    case '/':
    case '?':
        winObj.keys[39]->color(CHANGEKEYCOLOR);
        winObj.keys[39]->redraw();
        break;
    default:;
    }
}

void unshowColor(int chr)
{
    switch (chr)
    {
    case '1':
        winObj.keys[0]->color(KEYCOLOR);
        winObj.keys[0]->redraw();
        break;
    case '2':
        winObj.keys[1]->color(KEYCOLOR);
        winObj.keys[1]->redraw();
        break;
    case '3':
        winObj.keys[2]->color(KEYCOLOR);
        winObj.keys[2]->redraw();
        break;
    case '4':
        winObj.keys[3]->color(KEYCOLOR);
        winObj.keys[3]->redraw();
        break;
    case '5':
        winObj.keys[4]->color(KEYCOLOR);
        winObj.keys[4]->redraw();
        break;
    case '6':
        winObj.keys[5]->color(KEYCOLOR);
        winObj.keys[5]->redraw();
        break;
    case '7':
        winObj.keys[6]->color(KEYCOLOR);
        winObj.keys[6]->redraw();
        break;
    case '8':
        winObj.keys[7]->color(KEYCOLOR);
        winObj.keys[7]->redraw();
        break;
    case '9':
        winObj.keys[8]->color(KEYCOLOR);
        winObj.keys[8]->redraw();
        break;
    case '0':
        winObj.keys[9]->color(KEYCOLOR);
        winObj.keys[9]->redraw();
        break;
    case 'q':
    case 'Q':
        winObj.keys[10]->color(KEYCOLOR);
        winObj.keys[10]->redraw();
        break;
    case 'w':
    case 'W':
        winObj.keys[11]->color(KEYCOLOR);
        winObj.keys[11]->redraw();
        break;
    case 'e':
    case 'E':
        winObj.keys[12]->color(KEYCOLOR);
        winObj.keys[12]->redraw();
        break;
    case 'r':
    case 'R':
        winObj.keys[13]->color(KEYCOLOR);
        winObj.keys[13]->redraw();
        break;
    case 't':
    case 'T':
        winObj.keys[14]->color(KEYCOLOR);
        winObj.keys[14]->redraw();
        break;
    case 'y':
    case 'Y':
        winObj.keys[15]->color(KEYCOLOR);
        winObj.keys[15]->redraw();
        break;
    case 'u':
    case 'U':
        winObj.keys[16]->color(KEYCOLOR);
        winObj.keys[16]->redraw();
        break;
    case 'i':
    case 'I':
        winObj.keys[17]->color(KEYCOLOR);
        winObj.keys[17]->redraw();
        break;
    case 'o':
    case 'O':
        winObj.keys[18]->color(KEYCOLOR);
        winObj.keys[18]->redraw();
        break;
    case 'p':
    case 'P':
        winObj.keys[19]->color(KEYCOLOR);
        winObj.keys[19]->redraw();
        break;
    case 'a':
    case 'A':
        winObj.keys[20]->color(KEYCOLOR);
        winObj.keys[20]->redraw();
        break;
    case 's':
    case 'S':
        winObj.keys[21]->color(KEYCOLOR);
        winObj.keys[21]->redraw();
        break;
    case 'd':
    case 'D':
        winObj.keys[22]->color(KEYCOLOR);
        winObj.keys[22]->redraw();
        break;
    case 'f':
    case 'F':
        winObj.keys[23]->color(KEYCOLOR);
        winObj.keys[23]->redraw();
        break;
    case 'g':
    case 'G':
        winObj.keys[24]->color(KEYCOLOR);
        winObj.keys[24]->redraw();
        break;
    case 'h':
    case 'H':
        winObj.keys[25]->color(KEYCOLOR);
        winObj.keys[25]->redraw();
        break;
    case 'j':
    case 'J':
        winObj.keys[26]->color(KEYCOLOR);
        winObj.keys[26]->redraw();
        break;
    case 'k':
    case 'K':
        winObj.keys[27]->color(KEYCOLOR);
        winObj.keys[27]->redraw();
        break;
    case 'l':
    case 'L':
        winObj.keys[28]->color(KEYCOLOR);
        winObj.keys[28]->redraw();
        break;
    case ';':
    case ':':
        winObj.keys[29]->color(KEYCOLOR);
        winObj.keys[29]->redraw();
        break;
    case 'z':
    case 'Z':
        winObj.keys[30]->color(KEYCOLOR);
        winObj.keys[30]->redraw();
        break;
    case 'x':
    case 'X':
        winObj.keys[31]->color(KEYCOLOR);
        winObj.keys[31]->redraw();
        break;
    case 'c':
    case 'C':
        winObj.keys[32]->color(KEYCOLOR);
        winObj.keys[32]->redraw();
        break;
    case 'v':
    case 'V':
        winObj.keys[33]->color(KEYCOLOR);
        winObj.keys[33]->redraw();
        break;
    case 'b':
    case 'B':
        winObj.keys[34]->color(KEYCOLOR);
        winObj.keys[34]->redraw();
        break;
    case 'n':
    case 'N':
        winObj.keys[35]->color(KEYCOLOR);
        winObj.keys[35]->redraw();
        break;
    case 'm':
    case 'M':
        winObj.keys[36]->color(KEYCOLOR);
        winObj.keys[36]->redraw();
        break;
    case ',':
    case '<':
        winObj.keys[37]->color(KEYCOLOR);
        winObj.keys[37]->redraw();
        break;
    case '.':
    case '>':
        winObj.keys[38]->color(KEYCOLOR);
        winObj.keys[38]->redraw();
        break;
    case '/':
    case '?':
        winObj.keys[39]->color(KEYCOLOR);
        winObj.keys[39]->redraw();
        break;
    default:;
    }
}

auto windowInput::handle(int event) -> int
{
    if (event == FL_KEYDOWN || event == FL_KEYUP)
    {
        static int keycode;
        // keycode = Fl::event_key();
        keycode = *Fl::event_text();

        if (keycode == chr)
        {
            unshowColor(chr);
            if (charIndex != text.size())
            {
                chr = text[charIndex];
                showColor(chr);
                chrArr[11] = text[charIndex++];
                winObj.displayChar->value(chrArr.data());
                winObj.input->value(reinterpret_cast<char *>(&chr));
                return 1;
            }
            if (file >> text)
            {
                charIndex = 0;
                chr = text[charIndex];
                showColor(chr);
                chrArr[11] = text[charIndex++];
                winObj.displayText->value(text.c_str());
                winObj.displayChar->value(chrArr.data());
                winObj.input->value(reinterpret_cast<char *>(&chr));
                return 1;
            }
            {
                unshowColor(chr);
                winObj.displayText->value("恭喜过关, 请关闭窗口");
                file.close();
                return 1;
            }
        }
        else
        {
            winObj.input->value(reinterpret_cast<char *>(&keycode));
            return 1;
        }
    }
    return Fl_Input::handle(event);
}

auto makeWindow() -> windowObject &
{
    winObj.window = new Fl_Double_Window(1920, 1017, "打字练习");
    winObj.window->resizable(winObj.window);

    // 获取系统中的字体数目
    Fl::set_fonts("-*");

    if (winObj.window != nullptr)
    {
        winObj.window->box(FL_UP_BOX);
        winObj.window->color(static_cast<Fl_Color>(25));

        {
            winObj.box = new Fl_Box(80, 23, 1720, 512);
            winObj.box->labelfont(617);
            winObj.box->labelsize(53);
            winObj.box->labelcolor(static_cast<Fl_Color>(139));
        } // Fl_Box* box

        {
            winObj.input = new windowInput(276, 747, 1517, 84, "input:");
            winObj.input->labelfont(617);
            winObj.input->labelsize(72);
            winObj.input->textfont(617);
            winObj.input->textsize(72);
            winObj.input->align(static_cast<Fl_Align>(36));
        } // Fl_Input* input

        {
            winObj.displayText = new Fl_Output(83, 545, 1710, 84);
            winObj.displayText->box(FL_UP_BOX);
            winObj.displayText->labeltype(FL_NO_LABEL);
            winObj.displayText->labelfont(617);
            winObj.displayText->labelsize(72);
            winObj.displayText->textfont(617);
            winObj.displayText->textsize(72);
            winObj.displayText->value("按回车开始练习!");
        } // Fl_Text_Display* displayText

        {
            winObj.displayChar = new Fl_Output(83, 644, 1710, 84);
            winObj.displayChar->box(FL_UP_BOX);
            winObj.displayChar->labeltype(FL_NO_LABEL);
            winObj.displayChar->labelfont(617);
            winObj.displayChar->labelsize(72);
            winObj.displayChar->textfont(617);
            winObj.displayChar->textsize(72);
        } // Fl_Text_Display* displayChar

        {
            winObj.clock = new Fl_Clock(918, 849, 150, 150);
            winObj.clock->labelfont(617);
            winObj.clock->labelsize(24);
            winObj.clock->align((FL_ALIGN_CENTER));
        } // Fl_Clock* clock
        {
            for (int i = 0; i != 10; i++)
            {
                winObj.keys[i] =
                    new Fl_Box(i * 114 + 205, 24, 110, 100, nameArr[i]);
            }
            for (int i = 0; i != 10; i++)
            {
                winObj.keys[i + 10] =
                    new Fl_Box(i * 114 + 260, 126, 110, 100, nameArr[i + 10]);
            }
            for (int i = 0; i != 10; i++)
            {
                winObj.keys[i + 20] =
                    new Fl_Box(i * 114 + 300, 228, 110, 100, nameArr[i + 20]);
            }
            for (int i = 0; i != 10; i++)
            {
                winObj.keys[i + 30] =
                    new Fl_Box(i * 114 + 375, 330, 110, 100, nameArr[i + 30]);
            }
            for (int i = 0; i != 40; i++)
            {
                winObj.keys[i]->labelsize(LABELSIZE);
                winObj.keys[i]->labelcolor(LABELCOLOR);
                winObj.keys[i]->box(FL_UP_BOX);
                winObj.keys[i]->color(KEYCOLOR);
            }
            for (int i = 0; i != 21; i++)
            {
                winObj.keys[i + 40] =
                    new Fl_Box(keyArr[i].x, keyArr[i].y, keyArr[i].w,
                               keyArr[i].h, keyArr[i].key);

                winObj.keys[i + 40]->labelsize(LABELSIZE);
                winObj.keys[i + 40]->labelcolor(LABELCOLOR);
                winObj.keys[i + 40]->box(FL_UP_BOX);
                winObj.keys[i + 40]->color(KEYCOLOR);
            }
        }
        winObj.window->end();

        winObj.window->show();
    } // Fl_Double_Window* window

    return winObj;
}

2023-06-03 更新 v4

  1. 修复了两个连续字符只能打印1字符的bug
  2. 剔除了原键盘图层的图形box
  3. 代码重构, 用hashmap封装key对应的编码

头文件

// generated by Fast Light User Interface Designer (fluid) version 1.0308
#ifndef TYPING_H
#define TYPING_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 

struct windowInput : Fl_Input
{
    windowInput(int X, int Y, int W, int H, const char *l = nullptr)
        : Fl_Input(X, Y, W, H, l)
    {}
    auto handle(int event) -> int override;
};

struct windowObject
{
    Fl_Double_Window *window;
    windowInput *input;
    Fl_Output *displayText;
    Fl_Output *displayChar;
    Fl_Clock *clock;
    std::array<Fl_Box *, 61> keys;
};

auto makeWindow() -> windowObject &;

auto openFile(const char *fileName) -> bool;

#endif

下面是测试程序, 就显得十分简单了, 毕竟运行逻辑都在Cpp文件中了

#include "typingGUI.h"

auto main() -> int
{
    openFile("E:\\clangC++\\keytest.txt");
    makeWindow();

    return Fl::run();
}

cpp文件

// generated by Fast Light User Interface Designer (fluid) version 1.0308
#include "typingGUI.h"
#include 
#include 
#include 
#include 
#include 
#include 

#define KEYCOLOR 30
#define CHANGEKEYCOLOR 4
#define LABELSIZE 40
#define LABELCOLOR 7

namespace
{
windowObject winObj;

std::ifstream file;
std::string text;
std::array<char, 128> chrArr{"请输入: "};

struct keys
{
    const char *key;
    const int x;
    const int y;
    const int w;
    const int h;
};

std::array<keys, 61> keyArr{
    {{"1!", 205, 24, 110, 100},       {"2@", 319, 24, 110, 100},
     {"3#", 433, 24, 110, 100},       {"4$", 547, 24, 110, 100},
     {"5%", 661, 24, 110, 100},       {"6^", 775, 24, 110, 100},
     {"7&", 889, 24, 110, 100},       {"8*", 1003, 24, 110, 100},
     {"9(", 1117, 24, 110, 100},      {"0)", 1231, 24, 110, 100},
     {"q", 260, 126, 110, 100},       {"w", 374, 126, 110, 100},
     {"e", 488, 126, 110, 100},       {"r", 602, 126, 110, 100},
     {"t", 716, 126, 110, 100},       {"y", 830, 126, 110, 100},
     {"u", 944, 126, 110, 100},       {"i", 1058, 126, 110, 100},
     {"o", 1172, 126, 110, 100},      {"p", 1286, 126, 110, 100},
     {"a", 300, 228, 110, 100},       {"s", 414, 228, 110, 100},
     {"d", 528, 228, 110, 100},       {"f", 642, 228, 110, 100},
     {"g", 756, 228, 110, 100},       {"h", 870, 228, 110, 100},
     {"j", 984, 228, 110, 100},       {"k", 1098, 228, 110, 100},
     {"l", 1212, 228, 110, 100},      {";:", 1326, 228, 110, 100},
     {"z", 375, 330, 110, 100},       {"x", 489, 330, 110, 100},
     {"c", 603, 330, 110, 100},       {"v", 717, 330, 110, 100},
     {"b", 831, 330, 110, 100},       {"n", 945, 330, 110, 100},
     {"m", 1059, 330, 110, 100},      {",<", 1173, 330, 110, 100},
     {".>", 1287, 330, 110, 100},     {"/?", 1401, 330, 110, 100},
     {"~`", 90, 24, 110, 100},        {"_-", 1344, 24, 110, 100},
     {"+=", 1456, 24, 110, 100},      {"Backspace", 1570, 24, 226, 100},
     {"Tab", 90, 126, 165, 100},      {"{[", 1400, 126, 110, 100},
     {"}]", 1514, 126, 110, 100},     {"|\\", 1627, 126, 168, 100},
     {"CapsLock", 90, 228, 204, 100}, {"\" \'", 1438, 228, 110, 100},
     {"Enter", 1550, 228, 244, 100},  {"Shift", 90, 330, 280, 100},
     {"Shift", 1514, 330, 280, 100},  {"Ctrl", 90, 432, 208, 100},
     {"Win", 300, 432, 110, 100},     {"Alt", 414, 432, 110, 100},
     {"Space", 528, 432, 712, 100},   {"Alt", 1243, 432, 110, 100},
     {"Win", 1358, 432, 110, 100},    {"Type", 1472, 432, 110, 100},
     {"Ctrl", 1586, 432, 208, 100}}};

std::unordered_map<char, int> hashKeyMap{
    {'1', 0},  {'!', 0},  {'2', 1},  {'@', 1},  {'3', 2},  {'4', 3},  {'5', 4},
    {'6', 5},  {'7', 6},  {'8', 7},  {'9', 8},  {'0', 9},  {'q', 10}, {'w', 11},
    {'e', 12}, {'r', 13}, {'t', 14}, {'y', 15}, {'u', 16}, {'i', 17}, {'o', 18},
    {'p', 19}, {'a', 20}, {'s', 21}, {'d', 22}, {'f', 23}, {'g', 24}, {'h', 25},
    {'j', 26}, {'k', 27}, {'l', 28}, {';', 29}, {'z', 30}, {'x', 31}, {'c', 32},
    {'v', 33}, {'b', 34}, {'n', 35}, {'m', 36}, {',', 37}, {'.', 38}, {'/', 39},
    {'Q', 10}, {'W', 11}, {'E', 12}, {'R', 13}, {'T', 14}, {'Y', 15}, {'U', 16},
    {'I', 17}, {'O', 18}, {'P', 19}, {'A', 20}, {'S', 21}, {'D', 22}, {'F', 23},
    {'G', 24}, {'H', 25}, {'J', 26}, {'K', 27}, {'L', 28}, {':', 29}, {'Z', 30},
    {'X', 31}, {'C', 32}, {'V', 33}, {'B', 34}, {'N', 35}, {'M', 36}, {'<', 37},
    {'>', 38}, {'?', 39}, {'#', 2},  {'$', 3},  {'%', 4},  {'^', 5},  {'&', 6},
    {'*', 7},  {'(', 8},  {')', 9}};

int chr = 13;

int charIndex = 0;

} // namespace

auto openFile(const char *fileName) -> bool
{
    file.open(fileName);
    return file.is_open();
}

void showColor(int chr)
{
    winObj.keys[hashKeyMap[static_cast<char>(chr)]]->color(CHANGEKEYCOLOR);
    winObj.keys[hashKeyMap[static_cast<char>(chr)]]->redraw();
}

void unshowColor(int chr)
{
    winObj.keys[hashKeyMap[static_cast<char>(chr)]]->color(KEYCOLOR);
    winObj.keys[hashKeyMap[static_cast<char>(chr)]]->redraw();
}

auto windowInput::handle(int event) -> int
{
    if (event == FL_KEYDOWN)
    {
        static int keycode;
        keycode = static_cast<unsigned char>(*Fl::event_text());

        if (keycode == chr)
        {
            unshowColor(chr);
            if (charIndex != text.size())
            {
                chr = static_cast<unsigned char>(text[charIndex]);
                showColor(chr);
                chrArr[11] = text[charIndex++];
                winObj.displayChar->value(chrArr.data());
                winObj.input->value(reinterpret_cast<char *>(&chr));
                return 1;
            }
            if (file >> text)
            {
                charIndex = 0;
                chr = static_cast<unsigned char>(text[charIndex]);
                showColor(chr);
                chrArr[11] = text[charIndex++];
                winObj.displayText->value(text.c_str());
                winObj.displayChar->value(chrArr.data());
                winObj.input->value(reinterpret_cast<char *>(&chr));
                return 1;
            }
            {
                unshowColor(chr);
                winObj.displayText->value("恭喜过关, 请关闭窗口");
                file.close();
                return 1;
            }
        }
        else
        {
            winObj.input->value(reinterpret_cast<char *>(&keycode));
            return 1;
        }
    }
    return Fl_Input::handle(event);
}

auto makeWindow() -> windowObject &
{
    winObj.window = new Fl_Double_Window(1920, 1017, "打字练习");
    winObj.window->resizable(winObj.window);

    // 获取系统中的字体数目
    Fl::set_fonts("-*");

    if (winObj.window != nullptr)
    {
        winObj.window->box(FL_UP_BOX);
        winObj.window->color(static_cast<Fl_Color>(25));

        {
            winObj.input = new windowInput(276, 747, 1517, 84, "input:");
            winObj.input->labelfont(617);
            winObj.input->labelsize(72);
            winObj.input->textfont(617);
            winObj.input->textsize(72);
            winObj.input->align(static_cast<Fl_Align>(36));
        } // Fl_Input* input

        {
            winObj.displayText = new Fl_Output(83, 545, 1710, 84);
            winObj.displayText->box(FL_UP_BOX);
            winObj.displayText->labeltype(FL_NO_LABEL);
            winObj.displayText->labelfont(617);
            winObj.displayText->labelsize(72);
            winObj.displayText->textfont(617);
            winObj.displayText->textsize(72);
            winObj.displayText->value("按回车开始练习!");
        } // Fl_Text_Display* displayText

        {
            winObj.displayChar = new Fl_Output(83, 644, 1710, 84);
            winObj.displayChar->box(FL_UP_BOX);
            winObj.displayChar->labeltype(FL_NO_LABEL);
            winObj.displayChar->labelfont(617);
            winObj.displayChar->labelsize(72);
            winObj.displayChar->textfont(617);
            winObj.displayChar->textsize(72);
        } // Fl_Text_Display* displayChar

        {
            winObj.clock = new Fl_Clock(918, 849, 150, 150);
            winObj.clock->labelfont(617);
            winObj.clock->labelsize(24);
            winObj.clock->align((FL_ALIGN_CENTER));
        } // Fl_Clock* clock

        {
            for (int i = 0; i != 61; i++)
            {
                winObj.keys[i] =
                    new Fl_Box(keyArr[i].x, keyArr[i].y, keyArr[i].w,
                               keyArr[i].h, keyArr[i].key);

                winObj.keys[i]->labelsize(LABELSIZE);
                winObj.keys[i]->labelcolor(LABELCOLOR);
                winObj.keys[i]->box(FL_UP_BOX);
                winObj.keys[i]->color(KEYCOLOR);
            }
        }

        winObj.window->end();
        winObj.window->show();
    } // Fl_Double_Window* window

    return winObj;
}

这是最终效果:
2023-05-29 用 fltk gui库编写一个打字练习程序_第1张图片

看着像是 二十年前的程序, 对的, 这个GUI就是比较简朴, 低调, 不油腻.

其实就是丑, 我承认, 但第一次吗, 还有进步空间. 编译后大概430多k, 和终端程序没有明显区别.


总结

没什么好总结的, 简单使用而已, 一般来讲是足够了.

以上代码遵从 GNU GPL(GNU General Public License, GNU通用公共许可证).

你可能感兴趣的:(笔记,c++,FKTK,GUI)