Skip to content

Commit 7b775b4

Browse files
committed
Fix bugs in t9
1 parent 0ad0dc1 commit 7b775b4

3 files changed

Lines changed: 63 additions & 24 deletions

File tree

RemoteListener/T9.cpp

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ char textBuffer[1024] = { 0 };
1111
char phoneKeyBuffer[1024];
1212

1313
time_t lastPressTime = 0;
14+
time_t lastPhoneLetterTime = 0;
1415
int keyIndex = 0;
1516
int lastKey = -1;
1617

@@ -19,8 +20,12 @@ HWND windowHandle = 0;
1920
LPCWSTR text = L"";
2021
LPCWSTR hintText = L"";
2122
std::string hintTextStr = "";
22-
int hintTextIndex = 0;
23-
int hintTextLength = 0;
23+
int hintTextIndex1 = 0;
24+
int hintTextLength1 = 0;
25+
int hintTextIndex2 = 0;
26+
int hintTextLength2 = 0;
27+
int textIndex1 = 0;
28+
int textLength1 = 0;
2429
int t9WordIndex = 0;
2530
ULONGLONG lastTS = 0;
2631
bool isShowing = false;
@@ -70,7 +75,7 @@ void findT9Word()
7075
phoneKeyNode->next = 0;
7176
phoneKeyNode->children = {0};
7277

73-
Node* Children[31] = { 0 };
78+
Node* Children[NUM_CHILDREN_TO_GET] = { 0 };
7479
int start = 0;
7580
if (strlen(phoneKeyNode->word) > 0)
7681
{
@@ -80,9 +85,9 @@ void findT9Word()
8085
{
8186
start = 1;
8287
}
83-
getTenChildrenWithWords(wordNode, &Children[1]);
88+
getChildrenWithWords(wordNode, &Children[1]);
8489
int lastIndex = 0;
85-
for (int i = start; i < 30; i++)
90+
for (int i = start; i < NUM_CHILDREN_TO_GET; i++)
8691
{
8792
Node* currentNode = Children[i];
8893
if (currentNode && currentNode->word)
@@ -92,22 +97,36 @@ void findT9Word()
9297
}
9398
if (t9WordIndex > lastIndex)
9499
t9WordIndex = lastIndex;
95-
for (int i = start; i < 30; i++)
100+
for (int i = start; i < NUM_CHILDREN_TO_GET; i++)
96101
{
97102
Node* currentNode = Children[i];
98103
if (!currentNode)
99104
continue;
100105
if (currentNode && currentNode->word)
101106
{
102-
printf("currentNode: %s\n", currentNode->word);
103107
if (i == t9WordIndex)
104108
{
105-
hintTextIndex = hintTextStr.length() + 1;
106-
hintTextLength = strlen(currentNode->word);
109+
hintTextIndex1 = hintTextStr.length() + 1;
110+
hintTextLength1 = strlen(currentNode->word);
107111
selectedNode = currentNode;
112+
if (i == 0)
113+
{
114+
hintTextIndex2 = keyIndex;
115+
hintTextLength2 = 1;
116+
textIndex1 = 0;
117+
textLength1 = 0;
118+
}
119+
else
120+
{
121+
hintTextIndex2 = 0;
122+
hintTextLength2 = 0;
123+
textIndex1 = 0;
124+
textLength1 = strlen(t9Numbers);
125+
}
108126
}
109127
hintTextStr = hintTextStr + " " + currentNode->word;
110128
}
129+
111130
}
112131
if (selectedNode && selectedNode->word) {
113132

@@ -124,17 +143,18 @@ void findT9Word()
124143
strcpy_s(textBuffer, sizeof(textBuffer), selectedNode->word);
125144
}
126145

127-
if (hintTextIndex > 12)
146+
if (hintTextIndex1 > 12)
128147
{
129-
hintTextStr = hintTextStr.substr(hintTextIndex - 12);
130-
hintTextIndex = 12;
148+
hintTextStr = hintTextStr.substr(hintTextIndex1 - 12);
149+
hintTextIndex1 = 12;
131150
}
132151
}
133152

134153
void phoneKey(uint8_t key)
135154
{
136155
time_t currentTime = time(NULL);
137-
double seconds = difftime(currentTime, lastPressTime); //Get the time since the last keypress
156+
double seconds = difftime(currentTime, lastPhoneLetterTime); //Get the time since the last keypress
157+
lastPhoneLetterTime = currentTime;
138158
if (seconds < 2 && strlen(phoneKeyBuffer) > 0 && lastKey == key)
139159
{
140160
++keyIndex %= strlen(phoneKeys[key]); //Cycle through characters in the string
@@ -379,6 +399,7 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
379399
{
380400
time_t currentTime = time(NULL);
381401
double seconds = difftime(currentTime, lastPressTime); //Get the time since the last keypress
402+
double phoneKeySeconds = difftime(currentTime, lastPhoneLetterTime); //Get the time since the last keypress
382403
if (!isShowing && seconds < 2)
383404
{
384405
ShowWindow(windowHandle, SW_NORMAL);
@@ -387,25 +408,41 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
387408
isShowing = true;
388409
}
389410
Rectangle(hDC, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
390-
if (seconds < 2)
411+
if (seconds < 4)
391412
{
392413
SetTextColor(hDC, RGB(50, 230, 70));
393414
SelectObject(hDC, hintFont);
394415
hintDest.left = 0;
395416
DrawText(hDC, hintText, (int)wcslen(hintText), &hintDest, 0);
396417
SetTextColor(hDC, RGB(200, 60, 35));
397418

398-
if (hintTextIndex < wcslen(hintText))
419+
if (hintTextIndex1 < wcslen(hintText))
420+
{
421+
SIZE sz;
422+
GetTextExtentPoint32(hDC, hintText, hintTextIndex1, &sz);
423+
hintDest.left = sz.cx;
424+
DrawText(hDC, &hintText[hintTextIndex1], hintTextLength1, &hintDest, 0);
425+
}
426+
if (hintTextIndex2 < wcslen(hintText) && phoneKeySeconds < 2)
399427
{
400428
SIZE sz;
401-
GetTextExtentPoint32(hDC, hintText, hintTextIndex, &sz);
429+
GetTextExtentPoint32(hDC, hintText, hintTextIndex2, &sz);
402430
hintDest.left = sz.cx;
403-
DrawText(hDC, &hintText[hintTextIndex], hintTextLength, &hintDest, 0);
431+
DrawText(hDC, &hintText[hintTextIndex2], hintTextLength2, &hintDest, 0);
404432
}
405433
}
406434
SelectObject(hDC, font);
407435
SetTextColor(hDC, RGB(50, 230, 70));
436+
dest.left = 0;
408437
DrawText(hDC, text, (int)wcslen(text), &dest, 0);
438+
if (textIndex1 < wcslen(text))
439+
{
440+
SetTextColor(hDC, RGB(200, 60, 35));
441+
SIZE sz;
442+
GetTextExtentPoint32(hDC, text, textIndex1, &sz);
443+
dest.left = sz.cx;
444+
DrawText(hDC, &text[textIndex1], textLength1, &dest, 0);
445+
}
409446
CreateThread(nullptr, 0, [](void*) -> DWORD
410447
{
411448
Sleep(2000);

RemoteListener/Trie.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,17 @@ Node* getNthChildNode(Node* root, int &index, int depth)
6868
return NULL;
6969
}
7070

71-
Node** getTenChildrenWithWords(Node * root, Node** Children)
71+
Node** getChildrenWithWords(Node * root, Node** Children)
7272
{
73-
73+
const int MAX_DEPTH = 10;
7474
//memset(Children, 0, sizeof(Children));
7575
int foundChildren = 0;
76-
for (int i = 0; i < 10; i++)
76+
for (int i = 0; i < MAX_DEPTH; i++)
7777
{
7878
Node* child = 0;
7979
do
8080
{
81-
int _foundChildren = foundChildren;
81+
int _foundChildren = foundChildren;
8282
child = getNthChildNode(root, _foundChildren, i);
8383
if (child)
8484
{
@@ -88,9 +88,9 @@ Node** getTenChildrenWithWords(Node * root, Node** Children)
8888
{
8989
break;
9090
}
91-
if (foundChildren >= 30)
91+
if (foundChildren >= NUM_CHILDREN_TO_GET)
9292
{
93-
i = 10;
93+
i = MAX_DEPTH;
9494
break;
9595
}
9696

RemoteListener/Trie.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ void printTrie(Node * root, int level);
4747
// Prints the numTabs many tabs
4848
void printTabs(int numTabs);
4949

50-
Node** getTenChildrenWithWords(Node* root, Node** Children);
50+
Node** getChildrenWithWords(Node* root, Node** Children);
51+
52+
#define NUM_CHILDREN_TO_GET 120
5153

5254
#endif

0 commit comments

Comments
 (0)