summaryrefslogtreecommitdiff
path: root/noncore/applets/keyhelper/keyhelperapplet/misc/KeyRepeater.cpp
blob: 84a8a89231ba2203b23f92963386714f28fc4bd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "KeyRepeater.h"

KeyRepeater::KeyRepeater()
{
	qDebug("KeyRepeater::KeyRepeater()");
	m_pTimer = new QTimer(this);
	connect(m_pTimer, SIGNAL(timeout()), this, SLOT(autoRepeat()));
	init();
}

KeyRepeater::~KeyRepeater()
{
	qDebug("KeyRepeater::~KeyRepeater()");
	delete m_pTimer;
}

void KeyRepeater::start(int unicode, int keycode, int modifiers)
{
	m_unicode = unicode;
	m_keycode = keycode;
	m_modifiers = modifiers;
	if(m_mode == ENABLE){
		m_pTimer->stop();
		if(isRepeatable(keycode)){
			/* repeater start */
			m_pTimer->start(m_repeatdelay, TRUE);
		}
	}
}

void KeyRepeater::stop(int keycode)
{
	if(keycode == 0
		|| keycode == m_keycode
		|| isRepeatable(keycode) == false){
		m_pTimer->stop();
	}
}

void KeyRepeater::init()
{
	m_mode = ENABLE;
	m_repeatdelay = 400;
	m_repeatperiod = 60;
	m_disablekeys.append(0);
	m_disablekeys.append(Qt::Key_Escape);
	m_disablekeys.append(Qt::Key_Shift);
	m_disablekeys.append(Qt::Key_Control);
	m_disablekeys.append(Qt::Key_Alt);
	m_disablekeys.append(Qt::Key_Meta);
	for(int i=Qt::Key_F1; i<=Qt::Key_F35; i++){
		m_disablekeys.append(i);
	}
}

void KeyRepeater::clear()
{
	m_disablekeys.clear();
}

void KeyRepeater::reset()
{
	clear();
	init();
}

void KeyRepeater::setRepeatable(int keycode, bool enable)
{
	if(enable){
		QValueList<int>::Iterator it = m_disablekeys.find(keycode);
		if(it != m_disablekeys.end()){
			m_disablekeys.remove(it);
		}
	} else {
		if(m_disablekeys.contains(keycode) == false){
			m_disablekeys.append(keycode);
		}
	}
}

bool KeyRepeater::isRepeatable(int keycode)
{
	if(m_disablekeys.contains(keycode)){
		return(false);
	} else {
		return(true);
	}
}

void KeyRepeater::autoRepeat()
{
	/* key release event */
#if 0
	sendKeyEvent(
		m_unicode,
		m_keycode,
		m_modifiers,
		FALSE,
		TRUE);
	/* key press event */
	sendKeyEvent(
		m_unicode,
		m_keycode,
		m_modifiers,
		TRUE,
		TRUE);
#else
	emit keyEvent(
		m_unicode,
		m_keycode,
		m_modifiers,
		FALSE,
		TRUE);
	/* key press event */
	emit keyEvent(
		m_unicode,
		m_keycode,
		m_modifiers,
		TRUE,
		TRUE);
#endif
	/* start auto repeat */
	m_pTimer->start(m_repeatperiod);
#if 0
	qDebug("autoRepeat[%x][%x][%x]",
		m_unicode,
		m_keycode,
		m_modifiers);
#endif
}

void KeyRepeater::statistics()
{
	qWarning("KeyRepeater::statistics()");
	qWarning(" delay[%d] period[%d]", m_repeatdelay, m_repeatperiod);
	for(QValueList<int>::Iterator it=m_disablekeys.begin();
			it!=m_disablekeys.end(); ++it){
		qDebug(" disable[%x]", *it);
	}
}