반응형

It describes how to use the touch API in Android applications.


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
public class MainActivity extends AppCompatActivity {
 
    int[] id = new int[3];
    int[] x = new int[3];
    int[] y = new int[3];
    String result;
 
    TextView textView;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        textView = findViewById(R.id.textView);
    }
 
    @Override
    public boolean onTouchEvent(MotionEvent event) {
 
        // 3개의 포인터(터치)까지 허용
        int pointer_count = event.getPointerCount();
        if (pointer_count > 3)
            pointer_count = 3;
 
        // ACTION_POINTER_DOWN 과 ACTION_POINTER_UP 이벤트는 액션 값에 추가적인 정보가 인코딩 되어 있다.
        // 액션 값과 MotionEvent.ACTION_POINTER_INDEX_MASK를 & 연산을 하게 되면 눌리거나 떼어진 포인터의 인덱스 값을 알 수 있다.
        // public static final int ACTION_POINTER_INDEX_MASK
        // Bits in the action code that represent a pointer index, used with ACTION_POINTER_DOWN and ACTION_POINTER_UP.
        switch (event.getAction() & MotionEvent.ACTION_MASK)
        {
            case MotionEvent.ACTION_DOWN:
                result = "Single Touch Down:";
                id[0= event.getPointerId(0);
                x[0= (int)event.getX(0);
                y[0= (int)event.getY(0);
                result += "\n(" + x[0+ ", " + y[0+ ")";
                break;
 
            case MotionEvent.ACTION_POINTER_DOWN:
                result = "Multi Touch Down:";
                for (int i = 0; i < pointer_count; i++)
                {
                    id[i] = event.getPointerId(i);
                    x[i] = (int)event.getX(i);
                    y[i] = (int)event.getY(i);
                    result += "\n(" + id[i] + ": " + x[i] + ", " + y[i] + ")";
                }
 
                // Move는 싱글, 멀티 모두 ACTION_MOVE 하나로 처리
            case MotionEvent.ACTION_MOVE:
                result = "Touch Move:";
                for (int i = 0; i < pointer_count; i++)
                {
                    id[i] = event.getPointerId(i);
                    x[i] = (int)event.getX(i);
                    y[i] = (int)event.getY(i);
                    result += "\n(" + id[i] + ": " + x[i] + ", " + y[i] + ")";
                }
                break;
 
            case MotionEvent.ACTION_UP:
                result = "Single Touch Up:";
                id[0= event.getPointerId(0);
                x[0= (int)event.getX(0);
                y[0= (int)event.getY(0);
                result += "\n(" + x[0+ ", " + y[0+ ")";
                break;
 
            case MotionEvent.ACTION_POINTER_UP:
                result = "Multi Touch Up:";
                for (int i = 0; i < pointer_count; i++)
                {
                    id[i] = event.getPointerId(i);
                    x[i] = (int) event.getX(i);
                    y[i] = (int) event.getY(i);
                    result += "\n(" + id[i] + ": " + x[i] + ", " + y[i] + ")";
                }
                break;
 
            default:
                break;
        }
 
        textView.setText(result);
 
        return super.onTouchEvent(event);
    }
}



Run the app and touch the panel.


반응형
Posted by J-sean
:
반응형

It describes how to use the touch API in Android applications.


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
public class MainActivity extends AppCompatActivity {
 
    int x, y;
    TextView textView;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        x = -1;
        y = -1;
        textView = findViewById(R.id.textView);
    }
 
    @Override
    public boolean onTouchEvent(MotionEvent event) {
 
        switch (event.getAction())
        {
            case MotionEvent.ACTION_DOWN:
                x = Math.round(event.getX());
                y = Math.round(event.getY());
                textView.setText("Down position: " + x + ", " + y);
                break;
 
            case MotionEvent.ACTION_MOVE:
                x = Math.round(event.getX());
                y = Math.round(event.getY());
                textView.setText("Move position: " + x + ", " + y);
                break;
 
            case MotionEvent.ACTION_UP:
                x = Math.round(event.getX());
                y = Math.round(event.getY());
                textView.setText("Up position: " + x + ", " + y);
                break;
 
            default:
                break;
        }
 
        return super.onTouchEvent(event);
    }



Run the app and touch the panel.


반응형
Posted by J-sean
:
반응형

The SendInput function inserts the events in the INPUT structures serially into the keyboard or mouse input stream.

마우스 입력을 보낸다.


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
#include <Windows.h>
#include <iostream>
 
using namespace std;
 
void PrintMenu()
{
    cout << "--- Mouse sender ---" << endl << endl;
    cout << "0: End" << endl;
    cout << "1: Get cursor position" << endl;
    cout << "2: Send left button click" << endl;
    cout << "3: Send right button click" << endl;
    cout << "4: Send cursor move" << endl;
    cout << "Choose: ";
}
 
void GetCursorPosition()
{
    Sleep(2000);    // Wait for 2 seconds before get the cursor position.
 
    POINT cursorPos;
    GetCursorPos(&cursorPos);
 
    cout << "Current cursor position : (x: " << cursorPos.x << ", y: " << cursorPos.y << ")" << endl;
}
 
void SendLeftClick()
{
    Sleep(5000);
 
    INPUT input;
    ZeroMemory(&input, sizeof(INPUT));
    input.type = INPUT_MOUSE;
    input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
    SendInput(1&input, sizeof(INPUT));
 
    input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
    SendInput(1&input, sizeof(INPUT));
}
 
void SendRightClick()
{
    Sleep(5000);
 
    INPUT input;
    ZeroMemory(&input, sizeof(INPUT));
    input.type = INPUT_MOUSE;
    input.mi.dwFlags = MOUSEEVENTF_RIGHTDOWN;
    SendInput(1&input, sizeof(INPUT));
 
    input.mi.dwFlags = MOUSEEVENTF_RIGHTUP;
    SendInput(1&input, sizeof(INPUT));
}
 
void SendMove()
{
    int x, y;
    int screenWidth = GetSystemMetrics(SM_CXSCREEN);
    int screenHeight = GetSystemMetrics(SM_CYSCREEN);
 
    cout << "Screen size: (" << screenWidth << ", " << screenHeight << ")" << endl;
    cout << "[x: 0~" << screenWidth - 1 << ", y: 0~" << screenHeight - 1 << "]" << endl;
    cout << "x: ";
    cin >> x;
    cout << "y: ";
    cin >> y;
 
    INPUT input;
    ZeroMemory(&input, sizeof(INPUT));
    input.type = INPUT_MOUSE;
    input.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
    // If MOUSEEVENTF_ABSOLUTE value is specified, dx and dy contain normalized absolute coordinates
    // between 0 and 65,535. The event procedure maps these coordinates onto the display surface.
    // Coordinate (0,0) maps onto the upper-left corner of the display surface; coordinate (65535,65535)
    // maps onto the lower-right corner. In a multimonitor system, the coordinates map to the primary monitor.
    //
    // If the MOUSEEVENTF_ABSOLUTE value is not specified, dxand dy specify movement relative to the previous
    // mouse event(the last reported position).Positive values mean the mouse moved right(or down); negative
    // values mean the mouse moved left(or up).
    float dx = x * (65535.0f / (screenWidth - 1));
    float dy = y * (65535.0f / (screenHeight - 1));
    input.mi.dx = LONG(dx);
    input.mi.dy = LONG(dy);
    SendInput(1&input, sizeof(INPUT));
}
 
int main()
{
    int choice = 1;
 
    while (choice)
    {
        PrintMenu();
        cin >> choice;
 
        switch (choice)
        {
        case 1:
            GetCursorPosition();
 
            break;
        case 2:
            SendLeftClick();
 
            break;
        case 3:
            SendRightClick();
 
            break;
        case 4:
            SendMove();
 
            break;
        default:
 
            return 0;
        }
    }
 
    return 0;
}



Run the program and choose the event you want to send.


반응형
Posted by J-sean
: