⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gui_onkey.c

📁 uc/gui 3.90a 代码增强 增加按键自动重复和延迟加速
💻 C
字号:
/*
*********************************************************************************************************
*                                                uC/GUI
*                        Universal graphic software for embedded applications
*
*                       (c) Copyright 2002, Micrium Inc., Weston, FL
*                       (c) Copyright 2002, SEGGER Microcontroller Systeme GmbH
*
*              礐/GUI is protected by international copyright laws. Knowledge of the
*              source code may not be used to write a similar product. This file may
*              only be used in accordance with a license and should not be redistributed
*              in any way. We appreciate your understanding and fairness.
*
----------------------------------------------------------------------
File        : GUI_OnKey.c
Purpose     : Implementation of GUI_StoreKeyMsg
---------------------------END-OF-HEADER------------------------------
*/

#include "GUI_Protected.h"

#if GUI_WINSUPPORT
  #include "WM.h"
#endif

//lf 20071205 增加按键自动重复-------------------------
 #include "..\myapp\KB_driver.h"
//end----------------------------------

/*********************************************************************
*
*       Static data
*
**********************************************************************
*/

#if GUI_WINSUPPORT

static int _KeyMsgCnt;
static struct {
  int Key;
  int PressedCnt;
} _KeyMsg;

#endif

static int _Key;

//lf 20071205 增加按键自动重复-------------------------
static int _KeyPeriod	= KEY_REPEAT_PERIOD_DEFAULT;	//按键重复周期
static int _KeyTimeLast = 0;	//按键时间

void _KeyRepeat(){
	int timeDiff;
	timeDiff = GUI_GetTime() - _KeyTimeLast;
	if(timeDiff<0)
		timeDiff += 32767;
	if(timeDiff >= _KeyPeriod){
		//_KeyTimeLast = curTime;
	    _KeyMsgCnt++;
		_KeyPeriod -= KEY_REPEAT_PERIOD_DECREASE;
		if(_KeyPeriod < KEY_REPEAT_PERIOD_MIN)
            _KeyPeriod = KEY_REPEAT_PERIOD_MIN;
	}
}

/********************************************************************
*
*	KB_SetRepeatPeriod
*
*/
int KB_SetRepeatPeriod(int period){
	int key_period;
	key_period = _KeyPeriod;
	_KeyPeriod = period;
	return key_period;
}

/********************************************************************
*
*	KB_GetRepeatPeriod
*
*/
int KB_GetRepeatPeriod(){
	return _KeyPeriod;
}
//end----------------------------------


/*********************************************************************
*
*       Public data
*
**********************************************************************
*/

GUI_KEY_MSG_HOOK* GUI_pfKeyMsgHook;

/*********************************************************************
*
*       Public code
*
**********************************************************************
*/
/*********************************************************************
*
*       GUI_GetKey
*/
int GUI_GetKey(void) {
  int r = _Key;
  _Key = 0;
  return r;
}

/*********************************************************************
*
*       GUI_StoreKey
*/
void GUI_StoreKey(int Key) {
  if (!_Key) {
    _Key = Key;
  }
  GUI_X_SIGNAL_EVENT();
}

/*********************************************************************
*
*       GUI_ClearKeyBuffer
*/
void GUI_ClearKeyBuffer(void) {
  while (GUI_GetKey());
}

/*********************************************************************
*
*       GUI_StoreKeyMsg
*/
void GUI_StoreKeyMsg(int Key, int PressedCnt) {
  #if GUI_WINSUPPORT    /* If 0, WM will not generate any code */
  _KeyMsg.Key = Key;
  _KeyMsg.PressedCnt = PressedCnt;
  _KeyMsgCnt = 1;
//lf 20071205 增加按键自动重复-------------------------
  _KeyPeriod = KEY_REPEAT_PERIOD_DEFAULT;	//恢复按键重复时间
  _KeyTimeLast = GUI_GetTime();				//记录按键时间
//end--------------------------------------------------
  GUI_X_SIGNAL_EVENT();
  #else
    GUI_USE_PARA(PressedCnt);
    GUI_StoreKey(Key);
  #endif
}

/*********************************************************************
*
*       GUI_PollKeyMsg
*/
#if GUI_WINSUPPORT    /* If 0, WM will not generate any code */
int GUI_PollKeyMsg(void) {
  int r = 0;
  GUI_LOCK();
//lf 20071205 增加按键自动重复-------------------------
  if (!_KeyMsgCnt && _KeyMsg.PressedCnt)
	  _KeyRepeat();								//按键自动重复
  else
      _KeyPeriod = KEY_REPEAT_PERIOD_DEFAULT;	//恢复按键重复时间
//end----------------------------------------------------
  if (_KeyMsgCnt) {
    int Key;
//lf 20071205 增加按键自动重复-------------------------
    _KeyTimeLast = GUI_GetTime();				//记录按键时间
//end--------------------------------------------------
    _KeyMsgCnt--;
    Key = _KeyMsg.Key;
    WM_OnKey(Key, _KeyMsg.PressedCnt);
    if (_KeyMsg.PressedCnt == 1) {
      GUI_StoreKey(Key);
    }
    r = 1;              /* We have done something */
  }
  GUI_UNLOCK();
  return r;
}
#endif

/*********************************************************************
*
*       GUI_SendKeyMsg
*
* Purpose:
*   Send the key to a window using the window manager (if available).
*   If no window is ready to take the input, we call the store routine
*   and wait for somebody to poll the buffer.
*/
void GUI_SendKeyMsg(int Key, int PressedCnt) {
  #if GUI_WINSUPPORT    /* If 0, WM will not generate any code */
    if (!WM_OnKey(Key, PressedCnt)) {
      GUI_StoreKeyMsg(Key, PressedCnt);
    }
  #else
    GUI_StoreKeyMsg(Key, PressedCnt);
  #endif
}

/*************************** End of file ****************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -